最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Have CSS Class Added with `is_page()` WP function

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Jan 17, 2020 at 17:10 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges 1
  • please check the documentation developer.wordpress/reference/functions/is_page/… – Michael Commented Jan 17, 2020 at 18:41
Add a comment  | 

1 Answer 1

Reset to default 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"; ?>

发布评论

评论列表(0)

  1. 暂无评论