Make WordPress Core

Opened 9 years ago

Last modified 5 years ago

#31360 new defect (bug)

Missing active state classes for parent menu items

Reported by: donkelduffi's profile donkelduffi Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Menus Keywords:
Focuses: Cc:


wp_nav_menu() currently dont set current_post_parent(or other active state css classes) for all parent pages, when it has a category menu item with current_post_parent state.

Steps to reproduce:

You have following menu (theme menu):

  • Level 1.1 (Page)
    • Level 2.1 (Page)
      • Level 3.1 (Category: "Allgemein")
    • Level 2.2 (Page)
    • Level 2.3 (Page)
  • Level 1.2 (Page)

On "Level 3.1" you will see blog posts from the category "Allgemein".
Each blog post is linked to a detail page. (full blog post).

When you visit the full blog post page, Level 3.1 will get the class "current_post_parent". But Level 2.1 and Level 1.1 dont get any active states. Its missing.

i currently use following to fix it in my theme:

function me_fix_category( $menu )
    //parse output of wp_nav_menu
    $xml = new DOMDocument();
    $xml->loadXML( $menu );
    do {
        $fixed = false;
        //search for li tags
        $elems = $xml->getElementsByTagName( 'li' );
        foreach ( $elems as $el ) {
            //check if li has a active state
            if ( strstr( $el->getAttribute( 'class' ), 'current-post-parent' ) ) {
                //check parent li tag
                $parent = $el->parentNode->parentNode;
                $class = $parent->getAttribute( 'class' );
                if ( !strstr( $class, 'current-post-parent' ) ) {
                    //no active state for the parent? append class
                    $parent->setAttribute( 'class', $class ? $class . ' current-post-parent' : 'current-post-parent' );
                    $fixed = true;
    //do this, till nothing was fixed
    } while ( $fixed );
    return $xml->saveHTML();

Change History (1)

This ticket was mentioned in Slack in #core by jorbin. View the logs.

9 years ago

Note: See TracTickets for help on using tickets.