The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

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

  1. Raquel Wilson
    Posted 9 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.


  2. jkeyes
    Posted 9 years ago #

    I haven't tested this but something like this should work for you:

    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
    Posted 9 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