我正在尝试使用walker类向wordpress中的描述和子菜单添加包装,描述将仅显示第一个菜单,因此结构如下所示
EDIT
我已经能够将包装div添加到描述中,但不能同时添加到子菜单和菜单描述中,因此我正在寻求帮助,以便将包装div添加到菜单描述和子菜单(如果有)
<ul id="menu-main-menu" class="primary-nav">
<li id="menu-item-41" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children first">
<a href="http://link" class="menu-image-title-after menu-image-not-hovered">Menu 1</a>
<div class=\'MainMenuWrapper\'> <!-- NEW WRAPPER STARTS --->
<ul class="nav_desc"><li class="first"><img src="http://link/arrow.png"></li><li class="last">Menu description Goes here.</li></ul>
<ul class="sub-menu">
<li id="menu-item-44" class="menu-item menu-item-type-post_type menu-item-object-page first"><a href="http://link" class="menu-image-title-after menu-image-not-hovered"><img width="84" height="46" src="http://link/sub_menu_bg1.png" class="menu-image menu-image-title-after" alt="Image Kitchen">Kitchen</a></li>
</ul>
</div> <!-- NEW WRAPPER ENDS --->
</li>
</ul>
这是我当前使用的代码
class Description_Walker extends Walker_Nav_Menu
{
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
$classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$class_names = join(
\' \'
, apply_filters(
\'nav_menu_css_class\'
, array_filter( $classes ), $item
)
);
! empty ( $class_names )
and $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';
$output .= "<li id=\'menu-item-$item->ID\' $class_names>";
$attributes = \'\';
! empty( $item->attr_title )
and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
! empty( $item->target )
and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
! empty( $item->xfn )
and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
! empty( $item->url )
and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
// insert description for top level elements only
// you may change this
$description = ( ! empty ( $item->description ) and 0==$depth )
? \'<ul class="nav_desc"><li><img src="\'.get_bloginfo( \'template_url\' ).\'/images/arrow.png"></li><li>\' . esc_attr( $item->description ) . \'</li></ul>\' : \'\';
// $description = ( ! empty ( $item->description ) )
// ? \'<span class="nav_desc">\' . esc_attr( $item->description ) . \'</span>\' : \'\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. \'</a> \'
. $args->link_after
. $description
. $args->after;
// Since $output is called by reference we don\'t need to return anything.
$output .= apply_filters(
\'walker_nav_menu_start_el\'
, $item_output
, $item
, $depth
, $args
);
}
}