• Resolved navasahmed

    (@navasahmed)


    hello,
    is there any way to give active class on the menu if we are in the custom post type single page? default WordPress menu has that option by adding hook. but that hook is not working with this plugin. is there any other hooks has to work with this plugin.

    here is the working code for WordPress default menu.

    function change_page_menu_classes($menu)
    {
        global $post;
        if (get_post_type($post) == 'projects')
        {
            $menu = str_replace( 'current-menu-item', '', $menu ); // remove all current_page_parent classes
            $menu = str_replace( 'menu-item-299', 'current-menu-item', $menu );
            // add the current_page_parent class to the page you want
        }
        return $menu;
    }
    add_filter( 'nav_menu_css_class', 'change_page_menu_classes', 10,2 );

    Kindly please check this and let us know how we can solve this

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @navasahmed

    I need to discuss this with my development team. Hopefully, I will get back to you with the solution.

    Regards,
    Sumit

    Thread Starter navasahmed

    (@navasahmed)

    Hi @sumitsanadhya

    Thanks for the quick reply :).

    Could you please make it fast. we need to solve the issue ASAP. hopefully we will get the solution as soon as possible

    Thanks.

    Hi @navasahmed

    You can use nav_menu_link_attributes filter to update or change the attribute or link like classes, title, target, href etc.

    You can check our code in v4.0.0/inc/classes/class-walker.php on line 140

    // Set attributes on menu item link.
            $atts           = array();
            $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
            $atts['target'] = ! empty( $item->target ) ? $item->target : '';
            $atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
            $atts['href']   = ! empty( $item->url ) ? $item->url : '';
            $atts['class']  = 'rmp-menu-item-link';
            $atts['role']   = 'menuitem';
            $atts           = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

    You can use a filter hook-like

    add_filter( 'nav_menu_link_attributes', 'change_page_menu_classes', 10, 4 );
    
    function change_page_menu_classes( $atts, $item, $args, $depth )
    {
        global $post;
        if ( 'projects' === get_post_type($post) &&  299 === intval( $item->ID ) )
        {
            //add your code here
            $atts['class'] = str_replace( 'rmp-menu-item-link', 'rmp-menu-item-link current-menu-item', $atts['class']);
        }
        return $atts;
    }

    Let me know if you need more help.

    Regards,
    sumit

    Thread Starter navasahmed

    (@navasahmed)

    Hello @sumitsanadhya

    Thank you for the snippet. It works for the menu link. Adding an extra class to “li” or “div” is what I need to do. The new class does not need to be added to anchor tags. Please help me to add the new class to “li” or “div”.

    Thank you in advance

    Thread Starter navasahmed

    (@navasahmed)

    Hello @sumitsanadhya

    Any updates on above request. waiting for your reply.

    Thanks

    Hi @navasahmed

    Let me discuss this with the technical team. I will get back to you soon.

    Regards,
    sumit

    Hi @navasahmed

    Yesterday we release a new version of RM which is v 4.1.11 so please update. you can use the below-given code.

    add_filter( 'rmp_nav_item_class', 'change_page_menu_classes', 10, 2 );
    function change_page_menu_classes( $class_names, $item )
    {
        global $post;
        if ( 'projects' === get_post_type($post) &&  299 === intval( $item->ID ) )
        {
            //add your code here
           $class_names = str_replace( 'rmp-menu-item', 'rmp-menu-item current-menu-item', $class_names);
        }
        return $class_names;
    }

    Let me know if you need more help.

    Regards,
    sumit

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add active class on menu’ is closed to new replies.