Add classes to menus to indicate that an item has sub-items: `.menu-item-has-children` for `wp_nav_menu()` and `.page_item_has_children` for `wp_page_menu()`. props hotchkissconsulting. fixes #23834.

Built from https://develop.svn.wordpress.org/trunk@25602


git-svn-id: http://core.svn.wordpress.org/trunk@25519 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Helen Hou-Sandí 2013-09-24 16:01:09 +00:00
parent 1fda14e76b
commit 5760809319
2 changed files with 24 additions and 4 deletions

View File

@ -271,11 +271,22 @@ function wp_nav_menu( $args = array() ) {
// Set up the $menu_item variables
_wp_menu_item_classes_by_context( $menu_items );
$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$sorted_menu_items[$menu_item->menu_order] = $menu_item;
$sorted_menu_items = $menu_items_with_children = array();
foreach ( (array) $menu_items as $menu_item ) {
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
if ( $menu_item->menu_item_parent )
$menu_items_with_children[ $menu_item->menu_item_parent ] = true;
}
unset($menu_items);
// Add the menu-item-has-children class where applicable
if ( $menu_items_with_children ) {
foreach ( $sorted_menu_items as &$menu_item ) {
if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
$menu_item->classes[] = 'menu-item-has-children';
}
}
unset( $menu_items, $menu_item );
/**
* Filter the sorted list of menu item objects before generating the menu's HTML.

View File

@ -955,6 +955,11 @@ function walk_page_tree($pages, $depth, $current_page, $r) {
else
$walker = $r['walker'];
foreach ( (array) $pages as $page ) {
if ( $page->post_parent )
$r['pages_with_children'][ $page->post_parent ] = true;
}
$args = array($pages, $depth, $r, $current_page);
return call_user_func_array(array($walker, 'walk'), $args);
}
@ -1043,6 +1048,10 @@ class Walker_Page extends Walker {
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if( isset( $args['pages_with_children'][ $page->ID ] ) )
$css_class[] = 'page_item_has_children';
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_current_page->ancestors ) )