我想设置一个自定义菜单漫游器来删除无序列表,并将每个单独的链接包装在自定义菜单中div
要素所需的输出如下所示:
<div class="col-md-3"><a href="linkurl">Link Title</a></div>
这就是我迄今为止所尝试的:
<? wp_nav_menu( array( \'menu\' => \'Main\', \'container\' => \'\', \'items_wrap\' => \'%3$s\', \'link_before\' => \'<div class="col-md-3">\', \'link_after\' => \'</div>\' ) ); ?>
但是,上述代码正在生成此输出:
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-47">
<a href="linkurl"><div class="col-md-3">Link Title</div></a>
</li>
以下是我根据@shahar共享的资源更新的尝试:
<?php wp_nav_menu( array(\'menu\' => \'Main\', \'walker\' => new Footer_Walker()) ); ?>
在我的
functions.php
:
class Footer_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth=0, $args=array()) {
$output .= "<div>".esc_attr($item->label);
}
function end_el(&$output, $item, $depth=0, $args=array()) {
$output .= "</div>\\n";
}
}
使用菜单上方的代码根本不会显示。
<小时>Update: 以下是我在函数文件中使用的最后一段代码:
class Footer_Walker extends Walker_Nav_Menu {
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 .= "<div class=\'col-sm-6 col-md-3\'>";
$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 ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. \'</a></div>\'
. $args->link_after
. $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
下面是在模板中生成菜单的代码:
<?php wp_nav_menu(array(\'menu\' => \'Footer\', \'items_wrap\'=> \'%3$s\', \'walker\' => new Footer_Walker(), \'container\'=>false, \'menu_class\' => \'\', \'theme_location\'=>\'footer\', \'fallback_cb\'=>false )); ?>