Add Custom attributes WordPress nav item

I need to add custom attributes to single WordPress menu item.

<li id="menu-item-365" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-children-0">
<a href="http://www.mywebsite.com/whats-new/">WHATS NEW</a>
</li>

But I need this code like this

Read More
<li id="menu-item-365" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-children-0">
<a href="http://www.mywebsite.com/whats-new/" data-reveal-id="myModal1" data-animation="fade">WHATS NEW</a>
</li>

Can somebody please help?

Related posts

Leave a Reply

5 comments

  1. Filter this, targetting a specific menu id is fairly easy:

    Add the following to your functions.php file.

    add_filter( 'nav_menu_link_attributes', 'my_nav_menu_attribs', 10, 3 );
    function my_nav_menu_attribs( $atts, $item, $args )
    {
      // The ID of the target menu item
      $menu_target = 365;
    
      // inspect $item
      if ($item->ID == $menu_target) {
        $atts['data-reveal-id'] = 'myModal1';
        $atts['data-animation'] = 'fade';
      }
      return $atts;
    }
    
  2. <?php
    
    $defaults = array(
        'theme_location'  => '',
        'menu'            => '',
        'container'       => 'div',
        'container_class' => '',
        'container_id'    => '',
        'menu_class'      => 'menu',
        'menu_id'         => '',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'before'          => '',
        'after'           => '',
        'link_before'     => '',
        'link_after'      => '',
        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'depth'           => 0,
        'walker'          => ''
    );
    
    wp_nav_menu( $defaults );
    
    ?>
    
  3. For WordPress functions.php

    If you need to add attributes to more than one menu item:

    function add_specific_menu_atts( $atts, $item, $args ) {
    
    $menu_items = array(66,72);
    
    if (in_array($item->ID, $menu_items)) {
       $atts['data-ps2id-offset'] = '-75';
    }
    
    return $atts;
    }
    add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );
    
  4. Use jquery.

    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
    
        $(document).ready(function() {
            $('#menu-item-365').find('a').attr('data-reveal-id', 'myModal1');
            $('#menu-item-365').find('a').attr('data-animation', 'fade');
        });
    </script>