PHP if Condition not working

I’m trying to create a condition in WordPress that only calls the wp_head() on all pages except for the “About Us” page and “News Articles” pages. Here’s the code:

<?php if (!is_page('about-us') || !is_page('news-articles')) {wp_head();}?>

It seems to work great when I only use the command for one of the pages, but using an operator seems to mess things up. In some cases the pages load correctly, but the desired results are not happening. I’ve also double-checked to make sure that I’m referencing the pages correctly, and I’m confident that I am.

Related posts

Leave a Reply

4 comments

  1. You should be using the “AND” operator, not “OR”

    <?php if (!is_page('about-us') && !is_page('news-articles')) {wp_head();}?> 
    

    Because you want to display the WP head only if both conditions are true.

  2. Rather than an or || you need an and &&.

    <?php if (!is_page('about-us') && !is_page('news-articles')) {wp_head();}?>
    

    Otherwise, one or the other condition is always true. If it isn’t about-us, OR it isn’t news-articles, this still leaves the possibility in each case that it is the other, while still satisfying the condition. Instead by using a logical AND, you specify that it isn’t about-us and also isn’t news-articles.

  3. Well, look at what you’re saying.

    If it is not the “About Us” page, or if it is not the “News Articles” page…

    So, let’s say you’re on the “About Us” page. Obviously “not on About Us” is false, but just as obviously “not on News Articles” page is true.

    In fact, no matter what page you’re on, at least one of the conditions will be true, and therefore the entire OR-combined if statement is true.

    Use && instead.

  4. All the others are correct of course, but if you really like your OR you can apply a bit of logic (!x&&!x === !(x||x)):

    <?php if (!(is_page('about-us') || is_page('news-articles'))) {wp_head();}?>