admin管理员组文章数量:1277901
I was faced with a minor issue but can't solve it myself. I need to add the value on the variable after all loop iterations. And nothing problem, but I need to use this variable in the other file. for example:
while( have_posts() ) {
the_post();
$x = '';
$x++;
get_template_part( 'content', 'right' );
}
Now I need to get the $x value with iteration in content-right.php I try to declare a variable into this file but in this case no iteration. Is there any way to solve this?
I was faced with a minor issue but can't solve it myself. I need to add the value on the variable after all loop iterations. And nothing problem, but I need to use this variable in the other file. for example:
while( have_posts() ) {
the_post();
$x = '';
$x++;
get_template_part( 'content', 'right' );
}
Now I need to get the $x value with iteration in content-right.php I try to declare a variable into this file but in this case no iteration. Is there any way to solve this?
Share Improve this question asked Sep 24, 2021 at 18:18 Victor SokoliukVictor Sokoliuk 1397 bronze badges1 Answer
Reset to default 0You are re-initializing your $x
variable on every iteration of the loop. Maybe you want to move its initialization outside the loop? To get $x
variable value in the content-right.php
file you can declare it as global:
global $x;
$x = 0;
while( have_posts() ) {
the_post();
$x++;
get_template_part( 'content', 'right' );
}
Then you can use it in the content-right.php
file:
global $x;
# ... here you can use $x variable value
本文标签: How to declare a variable in a loop and make it available in the template file
版权声明:本文标题:How to declare a variable in a loop and make it available in the template file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741302012a2371143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论