Nav menu current item not highlihting

I was working on a site with categories as primary menu .I have a new post type and added an archive content as menu item with label ‘City Guide’.Now when I on City Guide page the menu item is not highlighting.Or with code the class ‘current’ is not printing in the menu item class.Here is my code:

 <?php
$menu_name = PRIMARY_NAVIGATION_NAME;
$menu_items = array();
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ){
  $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
  $menu_items = $menu ? wp_get_nav_menu_items($menu->term_id) : array();
}
?>
<nav class="visible-sm visible-md visible-lg" role="navigation">
  <ul id="menu-primary-navigation" class="main-navigation">
    <?php if( !empty($menu_items) ): ?>
      <?php
      $current_category_object = null;
      if( !empty($wp_query->query_vars['category_name']) ){
        $category_name = $wp_query->query_vars['category_name'];
        $current_category_object = get_category_by_slug($wp_query->query_vars['category_name']);
      }
      ?>
      <?php foreach( $menu_items as $item): ?>
        <?php
        $active_class = false;
        if ($item->object == 'category' && !empty($current_category_object) ){
          $cat_object = get_category($item->object_id);
          if ($cat_object->term_id == $current_category_object->term_id || $cat_object->term_id == $current_category_object->parent) {
            $active_class = true;
          }
        }
        ?>
        <li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class) echo ' current'?>">
          <a href="<?php echo $item->url; ?>" class="main-category"<?php if( $item->target) echo ' target="' . $item->target . '"'; ?>><?php echo $item->title ?></a>
          <?php if( $item->object == 'category'): ?>
            <?php
            $sub_categories = get_terms( array('category'), array('child_of' => $item->object_id, 'hide_empty' => 0) );
            if (count($sub_categories) <= 4) {
              include(locate_template('partials/sub-menu-two-featured.php'));
            } else {
              include(locate_template('partials/sub-menu-one-featured.php'));
            }
            ?>
          <?php endif; ?>

          <?php if ($item->title == 'City Guide'): ?>
            <?php include(locate_template('partials/sub-menu-city-guide.php')); ?>
          <?php endif; ?>
        </li>
        <?php wp_reset_query(); ?>
      <?php endforeach; ?>
    <?php endif; ?>
  </ul>
</nav>

How can i implement it into it using a condition to add current for my archive page menu item in the above code .

Read More

I tried

 <?php if ($item->title == 'City Guide'): ?> 

but it was echoing in all instance not when the menu is active.
Please help.

Related posts

2 comments

  1. One possible solution is to use get_queried_object() function for this.

    Codex says

    if you’re on a single post, it will return the post object
    if you’re on a page, it will return the page object
    if you’re on an archive page, it will return the post type object
    if you’re on a category archive, it will return the category object
    if you’re on an author archive, it will return the author object

    If you start using $qo = get_queried_object() then no need to use this code

        if( !empty( $wp_query->query_vars['category_name'] ) ){
            $category_name = $wp_query->query_vars['category_name'];
            $current_category_object = get_category_by_slug(
                  $wp_query->query_vars['category_name'] 
            );
        }
    

    because:

    • If you are in category-archive page, $qo will contain category object, and $qo->term_id and $qo->parent will be available.

    • If you are in CPT archive page, then $qo will contain CPT object, and properties like CPT name, query_var, label and so on will be available.

    So when you have custom queried object and with the help of some conditional tags like is_tax(), is_tag(), is_category(), is_post_type_archive() you will be able to determine exactly where you are.

    P.S. You must check the codex first, because there have some notes for precedence when using get_queried_object()

  2. <?php
    $active_class = false;
            $this_category = get_queried_object();
    $this_category->name;
    if ($this_category->name == 'city-guide' ){
     $active_class = true;
              } ?>
    <li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class ==1 && sanitize_title($item->title) =='city-guide') echo ' current'; ?>">
    

    this worked thanks @pgk for pointing me out

Comments are closed.