Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created September 5, 2012 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/3638943 to your computer and use it in GitHub Desktop.
Save jchristopher/3638943 to your computer and use it in GitHub Desktop.
WordPress Menu items that have children are not flagged as such until visiting the permalink of a child link. This gist forces a custom Walker on all calls to wp_nav_menu that adds a class to parents out of the box.
<?php
/**
* Iti_Walker_Flag_Parents
* Adds a class of 'menu-item-parent' to all WordPress Menu items that have children
*/
class Iti_Walker_Flag_Parents extends Walker_Nav_Menu
{
function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output)
{
$id_field = $this->db_fields['id'];
if (!empty($children_elements[$element->$id_field]))
{
$element->classes[] = 'menu-item-parent';
}
Walker_Nav_Menu::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
// callback that sets a Menu Walker argument to be an instantiation of Iti_Walker_Flag_Parents
function iti_set_flag_parents( $args )
{
return array_merge( $args, array(
'walker' => new Iti_Walker_Flag_Parents()
) );
}
// we're always going to add our custom Walker
add_filter( 'wp_nav_menu_args', 'iti_set_flag_parents' );
@jchristopher
Copy link
Author

Important to note that if implemented, this gist applies the Walker to all calls to wp_nav_menu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment