1

I have a nav <li> item where I add a page_active CSS class when a submenu page is visited. This works all OK when one of the pages is visited, but I can't get it to add the class when the second submenu page is visited.

With the code below if you visit the 'Our Team' submenu page the 'About' parent menu item has the CSS class page_active added.

<li class="td menu-item menu-item-2 underline-about <?php if (is_page('Our-Team')){echo "page_active";};?>">ABOUT
    <ul class="submenu about-submenu">
        <li class="submenu-item submenu-item-1"><a class="tl submenu-link" href="<?php echo esc_url(home_url('/our-team'));?>">Our Team</a></li>
        <li class="submenu-item submenu-item-2"><a class="tl submenu-link" href="<?php echo esc_url(home_url('/representation'));?>">Representation</a></li>
    </ul>
</li>

However because I want the CSS class to be added to the parent <li> item when you visit either submenu page, I thought I could do this by adding a simple || or operator in the is_page() function, but this doesn't work. I've tried the code below in the parent <li> item.

<?php if (is_page('Our-Team || 'Representation')){echo "page_active";};?>

How would i get it to add the 'page_active' class to the parent menu item on either/both pages?

1

1 Answer 1

2

Bitwise operators expect conditionals on either side of them to evaluate to either TRUE or FALSE. If FALSE it checks the next conditional until it finds a TRUE or all conditionals have been checked. Also, the is_page() function doesn't allow parameters to be passed in on a bitwise basis. So simply change your code to the below and you're good!

<?php if ( is_page('Our-Team') || is_page('Representation') ) echo "page_active"; ?>

1
  • Thanks Waldo. I'm relatively new to PHP.
    – pjk_ok
    Jan 17, 2020 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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