Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Custom Post type and WP Nav menus – Active State

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #46313
    Ricky55
    Member

    I have a custom post type called cakes.

    I am using a file called single-cakes.php to display this content.

    How do I get a class in my WP Nav menu so I can provide it with an active state?

    On a different site I found this code that worked but this was with a standard post type.

    add_filter(‘nav_menu_css_class’ , ‘special_nav_class’ , 10 , 2);
    function special_nav_class($classes, $item){
    if(is_single() && $item->title == “cakes”){
    $classes[] = “current_page_item”;
    }
    return $classes;
    }

    #142378
    Ricky55
    Member

    No one got any ideas on this? I could really do with a fix supposed to be going live tomorrow. I’ve search the web high and low but because I don’t fully understand the solutions its hard to know why they aren’t working for me.

    Thanks

    #142697
    Ricky55
    Member

    Finally found some code that worked for me.

    Included below just in case someone ever finds this thread and has the same issue.

    // Add class of current-page-ancestor to Custom Post Types
    function remove_parent($var)
    {
    if ($var == ‘current_page_parent’ || $var == ‘current-menu-item’ || $var == ‘current-page-ancestor’) { return false; }
    return true;
    }

    function tg_add_class_to_menu($classes)
    {
    // Add custom post type name
    if (is_singular(‘cakes’))
    {
    $classes = array_filter($classes, “remove_parent”);

    // Need to add menu item number
    if (in_array(‘menu-item-56’, $classes)) $classes[] = ‘current-page-ancestor’;
    }

    return $classes;
    }

    if (!is_admin()) { add_filter(‘nav_menu_css_class’, ‘tg_add_class_to_menu’); }

    #142698
    Ricky55
    Member

    Add to your functions.php

    #159562
    themuse
    Participant

    Yo @Ricky55, Your code works which is great, many thanks!

    #160750
    darcypaterson
    Participant

    I tried adding this code to my functions.php file but it gave me an error. Am I missing something? I changed the menu item id and the name… I’m stumped.

    // Add class of current-page-ancestor to Custom Post Types
    function remove_parent($var)
    {
    if ($var == ‘current_page_parent’ || $var == ‘current-menu-item’ || $var == ‘current-page-ancestor’) { return false; }
    return true;
    }

    function tg_add_class_to_menu($classes)
    {
    // Add custom post type name
    if (is_singular(‘work’))
    {
    $classes = array_filter($classes, “remove_parent”);

    // Need to add menu item number
    if (in_array(‘menu-item-21′, $classes)) $classes[] = ‘current-page-ancestor’;
    }

    return $classes;
    }

    if (!is_admin()) { add_filter(‘nav_menu_css_class’, ‘tg_add_class_to_menu’); }

    #171701
    natebeaty
    Participant

    This was very handy, thanks for posting. I did streamline it a bit:

    // custom nav highlighting for various pages
    function fb_custom_nav_highlights($classes) {
      if (is_singular('video') && in_array('nav-watch-now', $classes)) {
        $classes[] = 'current-menu-item';
      } elseif (is_singular('post') && in_array('nav-whats-new', $classes)) {
        $classes[] = 'current-menu-item';
      }
      return $classes;
    }
    if (!is_admin()) { add_filter('nav_menu_css_class', 'fb_custom_nav_highlights'); }
    
    #194820
    bdev
    Participant

    Thanks Ricky,
    I’ve been through various post and finally your’s work.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Other’ is closed to new topics and replies.