WordPress.org

Support

Support » Themes and Templates » How-To: Highlight Nav if Parent Is…

How-To: Highlight Nav if Parent Is…

  • Right now, I am using the following in the header template for my navbar:

    <li><a <?php if (is_page('about')) echo('class="current" '); ?>href="<?php bloginfo('url'); ?>/about/">about</a></li>

    What it does is highlight the link in the nav bar if the current page is “About”. However, I am wondering if there is a way to highlight the link for all pages whose parent is “About”.

    For example, if a subpage of the “About” parent page is “History”, how can I still highlight the “About” link on the navbar?

    I hope this makes sense. It’s a bit difficult to explain in writing.

    Here is the site with the navbar I’m trying to alter. It might illustrate my question better.

    http://www.youthbuild.parallelmvmt.com/

Viewing 2 replies - 1 through 2 (of 2 total)
  • I haven’t tested this but something like this should work for you:

    
    <?php
    if (is_page()) {
      // find root parent page
      $root = $post;
      while ($root->post_parent != 0) {
        $root = get_page($root->post_parent);
      }
      // highlight nav li
      if ($root->post_name == 'about') {
        echo('class="current" ');
      }
    }
    ?>
    

    Great. I’ll give it a try and let you know if it worked. Thanks for the help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How-To: Highlight Nav if Parent Is…’ is closed to new replies.