WordPress doesn't add parent
class to menu items that have submenus. Therefore this is required to extend this:
function add_parent_css( $classes, $item ) {
global $have_children;
if( $have_children ) {
$classes[] = 'parent';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'add_parent_css', 10, 2 );
class custom_walker extends Walker_Nav_Menu {
function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output){
$GLOBALS['have_children'] = (isset($children_elements[$element->ID]))? 1:0;
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
[...]
}
The code will insert parent
class to all menu items that have sub-menus and this can be then used in CSS like:
.parent:after { content: ">"; }
I'm wondering though, if there is a way to do that without custom menu walker? Just with filters? The reason I'm asking this is because some plugins like WPML ignore custom menu walkers for menu items and their "language switcher" in the menu doesn't get parent
class even if has sub-items. Instead of waiting for WPML to fix that I would prefer to just update the menu in a theme to do it in a simpler way.
wp_list_pages
,wp_nav_menu
etc.wp_nav_menu
but I would prefer to get rid of theme custom menu walker anyway. I would prefer it to haveparent
class via filter only.