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 .
I tried
<?php if ($item->title == 'City Guide'): ?>
but it was echoing in all instance not when the menu is active.
Please help.
One possible solution is to use
get_queried_object()
function for this.Codex says
If you start using
$qo = get_queried_object()
then no need to use this codebecause:
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 CPTname
,query_var
,label
and so on will be available.So when you have custom queried object and with the help of some
conditional tags
likeis_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()
this worked thanks @pgk for pointing me out