下面是一个学步的课程,可以做到这一点。将此添加到您的函数中。php文件;
class Description_Walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
$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 .= "";
$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 $class_names>"
. $args->link_before
. $title
. \'</a>\'
. $args->link_after
. $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
function header_nav()
{
wp_nav_menu(
array(
\'theme_location\' => \'header-menu\',
\'menu\' => \'\',
\'container\' => \'\',
\'container_class\' => \'\',
\'container_id\' => \'\',
\'menu_class\' => \'\',
\'menu_id\' => \'\',
\'echo\' => true,
\'fallback_cb\' => \'wp_page_menu\',
\'before\' => \'\',
\'after\' => \'\',
\'link_before\' => \'\',
\'link_after\' => \'\',
\'items_wrap\' => \'%3$s\',
\'depth\' => 0,
\'walker\' => new Description_Walker
)
);
}
最后,在您的主题标题中添加对nav header\\u nav()的调用,无论您想在哪里显示菜单。在这里,我用NAV元素对其进行了包装;
<nav class="" id="" role="navigation">
<?php header_nav(); ?>
</nav>
这应该输出这个;
<nav role="navigation" id="">
<a class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item" href="#">Link 1</a>
<a class="menu-item menu-item-type-taxonomy menu-item-object-category" href="#">link 2</a>
<a class="menu-item menu-item-type-taxonomy menu-item-object-category" href="#">link 3</a>
</nav>
资料来源:
https://stackoverflow.com/questions/12159486/wordpress-change-header-navigation-list-items-to-div/12161295#12161295