[wp-trac] [WordPress Trac] #31360: Missing active state classes for parent menu items

WordPress Trac noreply at wordpress.org
Tue Feb 17 13:59:11 UTC 2015


#31360: Missing active state classes for parent menu items
--------------------------+-----------------------------
 Reporter:  donkelduffi   |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  4.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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();
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31360>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list