[英]Add class to Wordpress Menu
我为每个Wordpress菜单项添加了类,但它们没有出现在代码中。
我的菜单:
<?php $wp_custom_nav = array(
'theme_location' => 'primary',
'container' => 'nav',
'container_class' => 'nav',
'echo' => false,
'fallback_cb' => false,
'items_wrap' => '%3$s',
'depth' => 0
);
echo strip_tags(wp_nav_menu( $wp_custom_nav ), '<nav><a>');
?>
我的function.php包括
register_nav_menus( array(
'primary' => esc_html__( 'Primary', 'yewtree' ),
) );
所以我有
<nav>
<a></a>
<a></a>
...
</nav>
但是没有我在Wordpress管理菜单中添加的任何课程。 这是为什么?
wp_nav_menu
默认使用Walker_Nav_Menu
。 这是负责打印课程的部分:
/**
* Filters the CSS classes applied to a menu item's list item element.
*
* @since 3.0.0
* @since 4.1.0 The `$depth` parameter was added.
*
* @param string[] $classes Array of 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 . '<li' . $id . $class_names . '>';
因此,正如您所看到的,这些类适用于<li>
标签而不适用于<a>
标签。
所以它们被应用了,但是......你这样做:
echo strip_tags(wp_nav_menu( $wp_custom_nav ), '<nav><a>');
因此,您除了<nav>
和<a>
之外的所有标记 - 所以<li>
标记被剥离(并且类也消失了)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.