admin管理员组文章数量:1330614
I have in wordpress functions.php file
This sample works with one variable only, but i want multiple variables
function variables() {
$var1 = 'lorem';
return $var1;
}
in the front template results "lorem":
echo variables();
works fine, but if i have many variables how can i displayed it?
example:
echo variables('var1');
echo variables('var2');
echo variables('etc');
i found a solution using global variables, but i don't like use that.
edit:
in functions.php add variables :
$some = "lorem 1"
$some2 = "lorem 2"
$some3 = "lorem 3"
then display in theme
<?php echo some(); ?>
<?php echo some2(); ?>
<?php echo some3(); ?>
I have in wordpress functions.php file
This sample works with one variable only, but i want multiple variables
function variables() {
$var1 = 'lorem';
return $var1;
}
in the front template results "lorem":
echo variables();
works fine, but if i have many variables how can i displayed it?
example:
echo variables('var1');
echo variables('var2');
echo variables('etc');
i found a solution using global variables, but i don't like use that.
edit:
in functions.php add variables :
$some = "lorem 1"
$some2 = "lorem 2"
$some3 = "lorem 3"
then display in theme
<?php echo some(); ?>
<?php echo some2(); ?>
<?php echo some3(); ?>
Share
Improve this question
edited Jul 12, 2020 at 2:53
Chris
asked Jul 12, 2020 at 2:46
ChrisChris
32 bronze badges
2
- What are you actually trying to do? – Jacob Peattie Commented Jul 12, 2020 at 2:50
- edited, sorry please refresh – Chris Commented Jul 12, 2020 at 2:53
1 Answer
Reset to default 0You have to think into the opposite direction: Don't pull the variables from the template, push them into it. Templates should be as simple as possible, the shouldn't know anything about the content of the rest of your code, for example function names.
In other words: use hooks.
In your template:
// the numbers are just context examples
do_action( 'something', 'one' );
do_action( 'something', 'two' );
do_action( 'something', 'three' );
In your functions.php
, you add a callback to that action:
// 1, 2, 3 are your variables
add_action( 'something', function( $context ) {
switch ( $context )
{
case 'one':
echo 1;
break;
case 'two':
echo 2;
break;
case 'three':
echo 3;
break;
default:
echo 'Context parameter is wrong!';
}
});
You can of course also echo more than one line and do some more complex stuff in the callback handler.
本文标签: get variables data from functionsphp to template wordpress (without global variables)
版权声明:本文标题:get variables data from functions.php to template wordpress (without global variables) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270446a2444201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论