Search code examples
phplogical-operators

Trouble with || operator


I have a page (titled PPC) and a custom post type (titled Presentations) that I don't want certain elements to appear on. I thought this code would work :

 <?php
      if ((!is_page('PPC')) || (!is_singular('presentations'))) {
      ?>
        <section id="menu" class="menu">
          <div class="grid grid--justify-center grid-md--justify-end grid--align-middle menu__inner">
            <nav role="navigation" class="menu__nav">
              <?php
              $nav_classes = 'header__menu header__menu--global';
              include(locate_template('components/component-nav.php', false, false));
              ?>
            </nav>
          </div>
        </section>

        <div class="header__item header__item--hamburger">
          <button id="toggleHeaderHamburger" onclick="toggleHeaderView()" class="hamburger">
            <span class="hamburger__bar"></span>
          </button>
        </div>

      <?php } ?>

Using each conditional by itself, without the ||, works fine. But when I combine them both and use the logical operator, nothing works. I've also tried taking the wrapping parentheses out as well, so it looks like this:

if (!is_page('PPC')) || (!is_singular('presentations')) {

But then I get an error. What am I doing wrong here?


Solution

  • As you've written it, !is_page('PPC') || !is_singular('presentations') would roughly translate it as "one of neither of these" which is a logical absurbity. Within the evaulation of that expression, if it's "not a PPC" it no longer matters whether or not it is "a presentation", and vice-versa.

    What you want is "either of these" is_page('PPC') || is_singular('presentations') prefixed with a "not" for:

    ! ( is_page('PPC') || is_singular('presentations') )

    "Not either of these".