Skip to content

Instantly share code, notes, and snippets.

@christophercochran
Created June 13, 2013 15:14
Show Gist options
  • Save christophercochran/5774513 to your computer and use it in GitHub Desktop.
Save christophercochran/5774513 to your computer and use it in GitHub Desktop.
Detect parent nav items and adds a class of "parent-menu-item" using Walker_Nav_Menu.
<?php
/**
* Updates the walker arg with custom class for detecting parent nav elements.
*/
function cc_add_menu_walker_arg( $args ) {
$args['walker'] = new CC_Detect_Parents();
return $args;
}
add_filter( 'wp_nav_menu_args', 'cc_add_menu_walker_arg' );
/**
* Detects menu items with children and adds class of parent-menu-item.
.
*/
class CC_Detect_Parents extends Walker_Nav_Menu {
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
$id_field = $this->db_fields['id'];
if ( is_object( $args[0] ) )
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
function start_el( &$output, $item, $depth, $args ) {
if ( $args->has_children )
$item->classes[] = 'parent-menu-item';
parent::start_el(&$output, $item, $depth, $args);
}
}
?>
.parent-menu-item > a::after {
content: '+';
padding-left: 20px;
padding-left: 2rem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment