【问题标题】:Can we add attribute for wordpress menu <li>我们可以为 wordpress 菜单添加属性 <li>
【发布时间】:2016-03-12 22:55:24
【问题描述】:

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

【问题讨论】:

  • 这 2 个链接可能对您有所帮助。 Link 1 , Link 2

标签: wordpress


【解决方案1】:

试试这个:

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;
}

教程 -> https://wpbeaches.com/adding-attribute-wordpress-menu-item/

【讨论】:

  • 这会在href而不是li上添加属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 2021-06-29
  • 2019-02-20
  • 2013-12-19
  • 2023-04-07
相关资源
最近更新 更多