wp_nav_menu change sub-menu class name?

前端 未结 13 2126
自闭症患者
自闭症患者 2020-11-28 02:44

Is there a way to change the child

    generated by WordPress itself to a custom class name?

    I know the parent

13条回答
  •  臣服心动
    2020-11-28 02:55

    Like it always is, after having looked for a long time before writing something to the site, just a minute after I posted here I found my solution.

    It thought I'd share it here so someone else can find it.

    //Add "parent" class to pages with subpages, change submenu class name, add depth class
    
        class Prio_Walker extends Walker_Nav_Menu {
        function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ){
            $GLOBALS['dd_children'] = ( isset($children_elements[$element->ID]) )? 1:0;
            $GLOBALS['dd_depth'] = (int) $depth;
            parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
        }
    
         function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent
      \n"; } } add_filter('nav_menu_css_class','add_parent_css',10,2); function add_parent_css($classes, $item){ global $dd_depth, $dd_children; $classes[] = 'depth'.$dd_depth; if($dd_children) $classes[] = 'parent'; return $classes; } //Add class to parent pages to show they have subpages (only for automatic wp_nav_menu) function add_parent_class( $css_class, $page, $depth, $args ) { if ( ! empty( $args['has_children'] ) ) $css_class[] = 'parent'; return $css_class; } add_filter( 'page_css_class', 'add_parent_class', 10, 4 );

    This is where I found the solution: Solution in WordPress support forum

提交回复
热议问题