我有以下菜单设置通过标题。php:
<?php
wp_nav_menu(
array(
\'container\' => false,
\'theme_location\' => \'primary\',
\'items_wrap\' => \'<ul class="header__nav-items">%3$s</ul>\',
)
);
这将在前端显示以下内容:
<ul class="header__nav-items">
<li class="header__nav-item menu-item-has-children">
<a href="#" class="header__nav-anchor">Parent</a><!-- I\'d like to replace this <a> tag with a <button> tag. -->
<ul class="header__sub-menu">
<li class="header__nav-item"><a href="child-page-one" class="header__nav-anchor">Child Page One</a></li>
<li class="header__nav-item"><a href="child-page-two" class="header__nav-anchor">Child Page Two</a></li>
<li class="header__nav-item"><a href="child-page-three" class="header__nav-anchor">Child Page Three</a></li>
<li class="header__nav-item"><a href="child-page-four" class="header__nav-anchor">Child Page Four</a></li>
</ul>
</li>
</ul>
我实现了一个自定义字段(
with ACF) 以便管理员可以通过WP admin选择菜单项是否为下拉菜单。
然后,我有以下via功能。php:
<?php
function edit_menu_item( $items, $args ) {
foreach( $items as $item ) {
if ( get_field( \'dropdown\', $item) == \'yes\' ) {
echo \'<button class="nav__button>\'.$item->title.\'</button>\';
}
}
return $items;
}
add_filter(\'wp_nav_menu_objects\', \'edit_menu_item\', 10, 2);
我希望上面的内容在前端输出以下内容:
<ul class="header__nav-items">
<li class="header__nav-item menu-item-has-children">
<button class="header__button">Parent</button>
<ul class="header__sub-menu">
<li class="header__nav-item"><a href="child-page-one" class="header__nav-anchor">Child Page One</a></li>
<li class="header__nav-item"><a href="child-page-two" class="header__nav-anchor">Child Page Two</a></li>
<li class="header__nav-item"><a href="child-page-three" class="header__nav-anchor">Child Page Three</a></li>
<li class="header__nav-item"><a href="child-page-four" class="header__nav-anchor">Child Page Four</a></li>
</ul>
</li>
</ul>
相反,按钮被放置在菜单之外,如下所示:
<button class="header__button">Parent</button><!-- Button appearing here -->
<ul class="header__nav-items">
<!-- Rest of the markup -->
我做错了什么?