0

I want to add custom attribute in WordPress menu item which has specific page slug. like this -

<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-101" id="menu-item-101"><a href="javascript:;">Meet The Therapist</a></li>

Change to

<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-101" id="menu-item-101" data-slug="$specific_page_slug"><a href="javascript:;">Meet The Therapist</a></li>

Thanks in advance.

2

It would be easier to add this on a menu link. The attributes for each link pass through the nav_menu_link_attributes filter.

You should be able to use it to add a custom one, along the lines of (not tested):

add_filter( 'nav_menu_link_attributes', function( $atts, $item ) {

 // check if $item is what you need

 $atts['data-slug'] = 'something';

 return $atts'

}, 10, 2);

To change the whole list items you would have to use more general walker_nav_menu_start_el filter, which only passes string you would have to cut up more messily.

  • Thanks, but still i do not know, how can i get slug name for each menu item. $atts['data-slug'] = 'something'; at the place of 'something' – shubham jain Apr 11 '16 at 12:08
  • Examine/dump $item and work your way from there. Your question is a little light on detail what you want and under which precise circumstances. – Rarst Apr 11 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.