我正在创建一个拆分菜单(一半项目位于左侧,徽标位于中间,其余项目位于右侧)。我不太了解Walkers但我发现this code snippet.
在我的客户想要添加子项之前,这对我来说很有效。当前代码正在计算所有菜单项(10),而我希望它只计算顶级项(当前为7)。
如何调整此代码,使其仅统计顶级项目?
class Split_Menu_Walker extends Walker_Nav_Menu {
var $current_menu = null;
var $break_point = null;
function start_el(&$output, $item, $depth, $args, $id=0) {
global $wp_query;
if( !isset( $this->current_menu ) )
$this->current_menu = wp_get_nav_menu_object( $args->menu );
if( !isset( $this->break_point ) )
$this->break_point = ceil( $this->current_menu->count / 2 ) + 1;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = \' class="\' . esc_attr( $class_names ) . \'"\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
$id = strlen( $id ) ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
if( $this->break_point == $item->menu_order )
$output .= $indent . \'</li></ul><ul><li\' . $id . $value . $class_names .\'>\';
else
$output .= $indent . \'<li\' . $id . $value . $class_names .\'>\';
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
$attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
$attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
$attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\';
$item_output = $args->before;
$item_output .= \'<a\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}