因为WordPress利用Walkers
您可以扩展类Walker_Nav_Menu
并将其实例传递给函数wp_nav_menu
.
因此,您的电话将是:
<?php wp_nav_menu( array(
\'menu\' => \'Main Menu\'
\'walker\' => new Custom_Nav_Walker()
) ); ?>
然后在一个单独的文件中创建类
Custom_Nav_Walker
那将是你的步行者。
该类扩展了Walker_Nav_Menu
那是start_el
和end_el
. 其中的代码与主类非常相似,只是我们引入了一些调整,用div
.
不幸的是,WordPress有时会将php和html代码混用在像这样的字符串中$output .= $indent . \'<li\' . $id . $class_names . \'>\';
在Walker_Nav_Menu::start_el
您将找到这行代码,它是所有列表项的打开标记(每次需要向列表中添加新项时,都会调用该方法)。
如果我们换成这个$output .= $indent . (0 === $depth ? \'<div \' : \'<li \' ) . $id . $class_names . \'>\';
我们将使用div
标记第一级的所有项目。
然后在方法中end_el
我们改变这个$output .= "</li>{$n}";
具有$output .= (0 === $depth ? "</div>{$n}" : "</li>{$n}");
所以我们可以关闭之前打开的div
标签
不幸的是,除非您深入了解复杂的regexp,否则这是实现目标的最佳快速解决方案。
此解决方案的问题是必须扩展Walker_Nav_Menu
类并重写上述两个方法,这意味着在WordPress更改某些内容时需要维护不必要的代码。
在这里,整个子类用作菜单的walker。
class Custom_Nav_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = [], $id = 0 ) {
if ( isset( $args->item_spacing ) && \'discard\' === $args->item_spacing ) {
$t = \'\';
$n = \'\';
} else {
$t = "\\t";
$n = "\\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
/**
* Filters the arguments for a single nav menu item.
*
* @since 4.4.0
*
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( \'nav_menu_item_args\', $args, $item, $depth );
/**
* Filters the CSS class(es) applied to a menu item\'s list item element.
*
* @since 3.0.0
* @since 4.1.0 The `$depth` parameter was added.
*
* @param array $classes The CSS classes that are applied to the menu item\'s `<li>` element.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
/**
* Filters the ID applied to a menu item\'s list item element.
*
* @since 3.0.1
* @since 4.1.0 The `$depth` parameter was added.
*
* @param string $menu_id The ID that is applied to the menu item\'s `<li>` element.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\' . $item->ID, $item, $args, $depth );
$id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . (0 === $depth ? \'<div \' : \'<li \' ) . $id . $class_names . \'>\';
$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 : \'\';
/**
* Filters the HTML attributes applied to a menu item\'s anchor element.
*
* @since 3.6.0
* @since 4.1.0 The `$depth` parameter was added.
*
* @param array $atts {
* The HTML attributes applied to the menu item\'s `<a>` element, empty strings are ignored.
*
* @type string $title Title attribute.
* @type string $target Target attribute.
* @type string $rel The rel attribute.
* @type string $href The href attribute.
* }
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args, $depth );
$attributes = \'\';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= \' \' . $attr . \'="\' . $value . \'"\';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( \'the_title\', $item->title, $item->ID );
/**
* Filters a menu item\'s title.
*
* @since 4.4.0
*
* @param string $title The menu item\'s title.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( \'nav_menu_item_title\', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= \'<a\' . $attributes . \'>\';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
/**
* Filters a menu item\'s starting output.
*
* The menu item\'s starting output only includes `$args->before`, the opening `<a>`,
* the menu item\'s title, the closing `</a>`, and `$args->after`. Currently, there is
* no filter for modifying the opening and closing `<li>` for a menu item.
*
* @since 3.0.0
*
* @param string $item_output The menu item\'s starting HTML output.
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
public function end_el( &$output, $item, $depth = 0, $args = [] ) {
if ( isset( $args->item_spacing ) && \'discard\' === $args->item_spacing ) {
$t = \'\';
$n = \'\';
} else {
$t = "\\t";
$n = "\\n";
}
$output .= (0 === $depth ? "</div>{$n}" : "</li>{$n}");
}
}
我保留了原始方法中的注释块,但如果不想了解有关过滤器的信息,可以将其删除。
班级Walker_Nav_Menu
位于中wp-includes/class-walker-nav-menu.php
.