Created
September 5, 2012 15:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important to note that if implemented, this gist applies the Walker to all calls to wp_nav_menu