Posted By

fjarrett on 09/29/10


Tagged

wordpress


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

baires
bobbym245
jamiebrwr


Assign Unique Classes To First And Last Menu Items


 / Published in: PHP
 

Use this function to filter your first and last menu items and assign them each a specific class name. This is very handy when applying separators in CSS but want to exclude the first and/or last items.

  1. // Assign Unique Classes To First And Last Menu Items
  2. function first_last_class( $items ) {
  3. $position = strrpos($items, 'class="menu-item', -1);
  4. $items=substr_replace($items, 'menu-item-last ', $position+7, 0);
  5. $position = strpos($items, 'class="menu-item');
  6. $items=substr_replace($items, 'menu-item-first ', $position+7, 0);
  7. return $items;
  8. }
  9. add_filter( 'wp_nav_menu_items', 'first_last_class' );

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wpCanyon on November 12, 2010

Awesome snippet :) Thanks.

You need to login to post a comment.