Can we add attribute for wordpress menu <li>

i have an HTML menu like this

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

Read More

I created this menu in wordpress. In that, submenu created as custom link. can I add data-content attribute for my wordpress submenu as shown in HTML code.

Related posts

1 comment

  1. Try this:

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

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

Comments are closed.