我正在尝试为我的WordPress主题添加自定义菜单结构。这就是我想要的外观:
<li>
<a href="link_to_parent_item" class="main-category" tabindex="3">Parent Name</a>
<div class="sub-menu">
<div class="sub-links pull-left">
<a href="link_to_child_item">Child Name1</a>
<a href="link_to_child_item">Child Name2</a>
</div>
</div>
</li>
我已经写了一个定制步行器来实现这一点,但它并没有给我真正想要的东西。这是我的学步课:
class loggmax_class_walker extends Walker_Nav_Menu{
// Start level, beginning tags
function start_lvl(&$output, $depth) {
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$output .= "\\n$indent";
$output .= \'<div class="sub-menu">\';
$output .= \'<div class="sub-links pull-left">\';
}
function end_lvl(&$output, $depth) {
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$output .= "$indent</div>";
$output .= \'</div>\';
}
// Start element (beginning list items)
function start_el( &$output, $item, $depth=0, $args=array(),$current_object_id=0 ) {
$this->curItem = $item;
$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_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
$id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$atts = array();
$atts[\'title\'] = ! empty( $item->attr_title ) ? $item->attr_title : \'\';
$atts[\'target\'] = ! empty( $item->target ) ? $item->target : \'\';
$atts[\'rel\'] = ! empty( $item->xfn ) ? $item->xfn : \'\';
$atts[\'href\'] = ! empty( $item->url ) ? $item->url : \'\';
$atts[\'slug\'] = ! empty( $item->slug ) ? $item->slug : \'\';
$atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args );
$attributes = \'\';
$item_output = \'\';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= \' \' . $attr . \'="\' . $value . \'"\';
}
}
// Check if it is a submenu
if ( $depth == 1 ) {
$output .= \'<a\'. $attributes. \' >\';
$output .= apply_filters( \'the_title\', $item->title, $item->ID );
$output .= \'</a>\';
} elseif ( $depth == 0 ) {
$item_output .= \'<li>\';
$item_output .= \'<a\'. $attributes. \' class="main-category">\';
$item_output .= apply_filters( \'the_title\', $item->title, $item->ID );
$item_output .= \'</a>\';
}
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
这个代码在我的标题中。php
if ( has_nav_menu( \'primary-menu\' ) )
wp_nav_menu( array(
\'theme_location\' => \'primary-menu\',
\'container\' => \'ul\',
\'menu_class\' => \'pull-left\',
\'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'walker\' => new loggmax_class_walker()
) );
这就是正在显示的内容:
<li>
<a href="link_here" class="main-category">Parent Title</a>
<div class="sub-menu">
<div class="sub-links pull-left">
<a href="link_here">Child Title</a></li>
<a href="link_here">Child Title 2</a></li>
</div>
</li>
如何删除结束列表标记
</li>
是否正在子菜单项中打印?