我开始使用WP主题,而且我希望用wp_nav_menu函数执行相似的菜单已超过一天了:
<div class="ui simple dropdown item">
CATEGORY NAME WITH SUB-CATEGORIES
<i class="dropdown icon"></i>
<div class="menu">
<a href="link_to_sub_category"><div class="item" >Sub Category 1</div></a>
<a href="link_to_sub_category"><div class="item" >Sub Category 2</div></a>
</div>
</div>
此外,如果没有子类别,HTML输出将如下所示:
<div class="ui simple dropdown item">
CATEGORY NAME WITHOUT SUB-CATEGORY
</div>
我需要在我的functions.php中添加什么内容? 任何提示或任何帮助将非常感谢!
感谢。
答案 0 :(得分:3)
将下一个代码添加到functions.php:
class SH_Nav_Menu_Walker extends Walker {
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent";
$output .= "<i class=\"dropdown icon\"></i>\n";
$output .= "<div class=\"menu\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</div>\n";
}
function start_el(&$output, $item, $depth, $args) {
$value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : array();
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', '', $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$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 . $id . $value . $class_names . '>';
$item_output .= '<div class="item">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</div>';
$item_output .= "</a>\n";
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
并使用下一个参数在“右侧”位置调用wp_nav_menu:
wp_nav_menu(array('items_wrap' => '<div id="%1$s" class="%2$s ui simple dropdown item">%3$s</div>', 'theme_location' => 'sidebar_right_menu', 'walker' => new SH_Nav_Menu_Walker))