WordPress.org

Support

Support » How-To and Troubleshooting » !is_page( ) not working when using OR ‘ || ‘

!is_page( ) not working when using OR ‘ || ‘

  • I am trying to use the !is_page() function to exclude certain pages from using a bit of code in the sidebar.

    This works:

    <?php if (!is_page(8)) { ?>

    -code here-

    <?php } ?>

    However when I try to add other pages using the OR function like this it does not work:

    <?php if ((!is_page(8)) || (!is_page(6))) { ?>

    -code here-

    <?php } ?>

    I have also tried this:

    <?php if (!is_page(8) || !is_page(6)) { ?>

    -code here-

    <?php } ?>

    Neither of them will work. Does anyone know how to fix this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The answer is that your logic is broken… The answer will always be true so your code will always run…

    If its page 6 or 8, then one of the !is_page(xx) is true, so the result is true.

    If its any other page e.g. 7, then both parts are true, so the result is true.

    If my brain’s working right, you want an AND instead.

    Mmm…

    Logically I want the code to show when it’s NOT page 6 OR page 8, OR any number of other pages.

    The logic is I want this code to show only when its not one of the page numbers that I put into !is_page(x).

    I actually have 13 pages that have specific sidebar content and if its not one of those 13 pages I want the -code here- that is shown above.

    I want to exclude those pages from possibly showing this code. Perhaps I am using an incorrect function and there is a better, more elegant WP solution?

    Hopefully my explanation is making sense.

    Yep, the way you’ve done it originally, an AND is the way to go.

    If its not page 8 and its not page 6 then show stuff.

    If you really want to use an OR, use the method you’ve got already…

    If its not (page 6 or page 8) then do the code, so either:

    <?php if ( !( is_page(8) || is_page(6) ) ) { ?>

    or this

    <?php if ( ( !is_page(8) ) && ( !is_page(6) ) ) { ?>

    should both work as equivalents…AND do what you’re looking for.

    This was perfect:

    <?php if ( !( is_page(8) || is_page(6) ) ) { ?>

    Thanks for helping work out the logic!

    No problem. Its one of my freaky talents…my brain may be mush, but its logical…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘!is_page( ) not working when using OR ‘ || ‘’ is closed to new replies.