0

I want an element to appear within the Loop, but only at certain points. I can supply the condition - in the form of a "flag" that gets set when the condition is fulfilled, preventing further iteration - directly in a template file, and it works, but when I produce the identical logic via function the flag does not seem to be set.

In other words - providing a simplified (the simplest!) case - the following works - CASE 1:

while( have_posts() ) : the_post();

if ( $flag < 1 ) {
    echo 'Flag worked!';
    $flag = 1;
}

/* all of the code producing a post */

endwhile;

The above works as expected: "Flag worked!" appears once and never again.

If, however - CASE 2 - I write a function, like so, and add it to the theme functions.php...

function flag_it() {
   if ( $flag < 1 ) {
       echo 'Flag worked!';
       $flag = 1;
   }
}

...and then place it in the exact same place as the first code, like so:

while( have_posts() ) : the_post(); 

flag_it();

/* all of the code producing a post */

endwhile;

The words "Flag worked" are turned into a lie, and appear before every post in the loop. In other words, the $flag value is not assessed by the if conditional, or the value of $flag continues to be treated as NULL ( so, "$flag < 1" remains true).

I've tried variations on the theme - with TRUE v FALSE, returned variables, value declared as 0, etc. - but none works, or, if it did!, would answer the basic question: Why does case 1 work, but not case 2, and is there any way to make case 2 or something like it - flagged condition supplied via function - work?

2
  • If you mean pre-declaring value of $flag as '0' - before "while" - no, it doesn't make the function work as desired. (If the solution requires multiple functions, that would be OK, but a solution that can't be supplied via external functions and instead requires adding code to the theme file won't really be an improvement over Case 1.)
    – CK MacLeod
    Commented Apr 4, 2016 at 16:22
  • Your approach is just bad which is why you ended up in trouble in the first place. you should not relay on global values ever.If you need a flag that can be mutated by a function you just pass it by reference to it. Static variables as in the answer are just a way to hide the global, but it is still a global and you should try not to use it unless there is no other way Commented Apr 4, 2016 at 17:24

1 Answer 1

0

The problem is that whenever you call flag_it() there's no $flag available, so it's always going to result in "flag worked". Try using a static variable instead:

function flag_it() {
    static $flag;
    if ( isset( $flag ) )
        return;

    $flag = true;
    echo 'Flag worked!';
}

Note that no matter how many times you call this function in a run, it will always echo just once. If you want to reset the counter you'll need to change it to accept some sort of $reset argument.

You can read more about variable scopes in PHP here: http://php.net/manual/en/language.variables.scope.php

4
  • Thanks - I'm going to mark this question as answered - since your answer does work, and since I'm trusting that the explanation appears at the site you've linked - and will explain why CASE 1 works and CASE 2 doesn't. Will also have to see how it works in more complicated "real life" case with further conditions.
    – CK MacLeod
    Commented Apr 4, 2016 at 16:45
  • Ah - I see now - was expecting it to work like other languages! Answer to underlying question would SEEM to be something along lines of "when you write the code directly into the template file, variable remains accessible within scope of whatever larger function, but, when you write as function, scope exists only within that function unless extended beyond it, for instance by declaring as static variable."
    – CK MacLeod
    Commented Apr 4, 2016 at 16:52
  • Note that a static variable doesn't really extend the scope beyond the function where it's declared, it's still local to the function and you still can't access it outside of that function, but static causes it to not lose its value when execution leaves the scope.
    – kovshenin
    Commented Apr 4, 2016 at 17:28
  • Understood - and thank you for the clarification. At the risk of further misuse of terms, one could say that the static variable doesn't extend the scope beyond the function, but it does extend or maintain it beyond a single iteration of the function. In any event, method works fine in more complex cases - so, even if is this seems to be an elementary question for PHP more than for WordPress, I'm grateful for this lesson. (Inspiration for this question originated in work customizing one of your own old themes - Expound - by the way!)
    – CK MacLeod
    Commented Apr 4, 2016 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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