0

I use custom page menus and category list. The theme I use includes css styles to highlight the menu and archive. My goal is to highlight them for a single post. For example, the link structure I use is: example.com/category/ The menu or category list is highlighted when I enter this page. example.com/category/car-modified However, the menu and category no longer remain highlighted here. I tried to do that from the Functions.php file. I tried the following code, but it didn't work. This code emphasizes the category of car everywhere. The car category is not hidden when it is not active. How can we improve this code for this?

function add_current_nav_class($classes, $item) {
    // Getting the current post details
    global $post;
    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));
    $current_post_type_slug = $current_post_type->rewrite['slug'];

    // Getting the URL of the menu item
    $menu_slug = strtolower(trim($item->url));

    // If the menu item URL contains the current post types slug add the current-menu-item class
    if (strpos($menu_slug,$current_post_type_slug) !== false) {
    $classes[] = 'current-menu-item';
    }
    else if( is_single() && $item->title == 'Car' )
    {
    // Blog page set active menu
    $classes[] = 'current-menu-item';
    }
    // Return the corrected set of classes to be added to the menu item
    return $classes;
    }
    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
  • Is there a "current-menu-parent" class added to the parent when you are on the single post page? If so you can just use CSS to target the child link. – RiddleMeThis Dec 14 '18 at 20:16
  • No. I can't use the parental menu. – A. Muller Dec 14 '18 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.