0

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?

0

You 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.