• Hi

    I’m create a site with a menu like the following structure

    • page 1
    • page 1.1
    • page 1.2
    • page 1.3
    • page 4
    • page 2
    • page 2.1
    • page 2.2
    • page 2.3
    • page 4
    • page 3
    • page 3.1
    • page 3.2
    • page 3.3
    • page 4

    Please notice “page 4” is added in all 3 branches of the menu.
    Now when I go to “page 4” in whatever way, the menu shows all 3 branches active (as it’s parent).
    Technically speaking the page indeed has 3 parents, but is there any way to only show the direct parent(s) path as active?

    Thank you for any suggestions

    best regards
    Hans Santens

    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens.
    • This topic was modified 9 years, 1 month ago by hsantens. Reason: indeting mnu structure
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,
    Please share the website url to me. I will give to any solution.
    Thanks
    Mohammad

    Thread Starter hsantens

    (@hsantens)

    Hi,

    Thank you for your response

    This is the url: http://hendrickx-books.dreamhosters.com/ (site is in Dutch)

    You can see the issue on the “rechten” page.
    If you navigate to “2015 – Heden” -> “2017 – Elvis Compleet” -> “Rechten”, you will see 3 other sites are marked as parent.
    Also the “sub-navigation” in the right sidebar doesn’t know were we are anymore. It looks like ik shows the navigation of the first occurrence of the “rechten” page.

    regards
    Hans

    Hi,
    Please paste the following code to functions.php of child theme.

    add_filter('nav_menu_css_class', 'remove_active_nav_menu_css_class');
    function remove_active_nav_menu_css_class($classes) {
        if(!empty($classes)) {  
            unset($classes);
            $classes = array('active');
        }
        return $classes;
    }
    

    Thanks
    Mohammad

    Thread Starter hsantens

    (@hsantens)

    Thanks
    This seems to remove all active attributes. However I would like to keep the “real” active path.

    Hi,
    Now paste this code at functions.php of child theme.

    function wpdocs_add_menu_parent_class( $items ) {
        $parents = array();
     
        // Collect menu items with parents.
        foreach ( $items as $item ) {
            if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
                $parents[] = $item->menu_item_parent;
            }
        }
     
        // Add class.
        foreach ( $items as $item ) {
            if ( in_array( $item->ID, $parents ) ) {
                $item->classes[] = 'menu-parent-item';
            }
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_objects', 'wpdocs_add_menu_parent_class' );
    

    Thanks
    Mohammad

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Menu parent items’ is closed to new replies.