我们可以为wordpress menu <li>添加属性吗?

时间:2015-12-08 05:41:21

标签: wordpress

我有一个像这样的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 &nbsp <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代码所示。

1 个答案:

答案 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/