WordPress.org

Ready to get started?Download WordPress

Forums

How-To: Highlight Nav if Parent Is... (3 posts)

  1. Raquel Wilson
    Member
    Posted 7 years ago #

    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/

  2. jkeyes
    Member
    Posted 7 years ago #

    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" ');
      }
    }
    ?>
    
  3. Raquel Wilson
    Member
    Posted 7 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic