我有一个像这样的HTML菜单
<ul id="menu-main-menu" class="menu">
<li class="current_page_item"> <a href="index.html"> Home </a> </li>
<li class="menu-item-simple-parent"> <a href="aboutus.html">About us   <i class="fa fa-caret-down"></i></a>
<ul class="sub-menu" style="left:0px;width:340px;">
<li data-content="mission"> <a href="#">Mission</a> </li>
<li data-content="vision"> <a href="#">Vision</a> </li>
<li data-content="management"> <a href="#">Management</a> </li>
</ul>
</li>
我在wordpress中创建了这个菜单。在那里,子菜单创建为自定义链接。我可以为我的wordpress子菜单添加数据内容属性,如HTML代码所示。
答案 0 :(得分:2)
试试这个:
add_filter( 'nav_menu_link_attributes', 'themeprefix_menu_attribute_add', 10, 3 );
function themeprefix_menu_attribute_add( $atts, $item, $args )
{
// Set the menu ID
$menu_link = 1215;
// Conditionally match the ID and add the attribute and value
if ($item->ID == $menu_link) {
$atts['data-toggle'] = 'modal';
}
//Return the new attribute
return $atts;
}
教程 - &gt; https://wpbeaches.com/adding-attribute-wordpress-menu-item/