• Resolved PBWordpressDP

    (@pbwordpressdp)


    This statement for some reason doesn’t work as expected. It doesn’t output ‘News’ but continues to output ‘Uncategorized’. Why? How to make the ‘OR’ statements work.

    if ($cat->cat_name != ‘NEWS’ || $cat->cat_name != ‘Uncategorized’)

Viewing 4 replies - 1 through 4 (of 4 total)
  • First, which is correct, ‘NEWS’ as in the conditional or ‘News’ as in your description?

    If either condition is TRUE then the whole statement is TRUE (inclusive OR). That is how OR works. So think about what happens when information gets fed into this. If your category is ‘Dog’ then both conditions are true and the code inside the ‘if’ runs, which I think is what you want. The category is neither ‘NEWS’ nor ‘Uncategorized’.

    If your category is ‘NEWS’, the first is FALSE but the second is TRUE, so it still fires. The reverse is true as well. If your category is ‘Uncategorized’ the first condition is TRUE and the second FALSE, so it fires.

    Since you only have one cat_name at a time that conditional is always going to be true. I am puzzled why it filters ‘NEWS’ at all, as you say it does. I just tested the logic and works as I say it does. Whatever the category name that ‘if’ is true.

    Long story ?? but I think what you want is:

    if (!($cat->cat_name != 'NEWS' && $cat->cat_name != 'Uncategorized')) {

    Thread Starter PBWordpressDP

    (@pbwordpressdp)

    Thanks for your response.

    I have a number of posts,listed in the categories section. I have managed to write a loop that echos all the categories onto the page. But I do not want ‘Uncategorized’ or ‘NEWS’ to be displayed. So inside the while loop, I wrote an if statement, so that it would not echo these to the web page.

    So:
    //loop starts
    if ($cat->cat_name != ‘NEWS’ || $cat->cat_name != ‘Uncategorized’) {

    echo $cat->cat_name;
    }
    //loop ends

    Result:
    All the categories are listed except ‘NEWS’.

    Result I am trying to get:
    All the categories should be listed except ‘NEWS’ and ‘Uncategorized’

    Basically backward from how I understood you. Sorry. Easy fix though. Remove the first “!” in my code above.

    if ($cat->cat_name != 'NEWS' && $cat->cat_name != 'Uncategorized') {

    Now if your category is ‘NEWS’ or ‘Uncategorized’ the conditional as a whole is FALSE. Any other values for cat_name and it will be TRUE. Kinda weird to use ‘and’ to get an ‘or’ but think it through and it makes sense.

    Thread Starter PBWordpressDP

    (@pbwordpressdp)

    Thank you very much. That solved the problem! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Simple OR statement not working’ is closed to new replies.