Assign Unique Classes To First And Last Menu Items


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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

You need to login to post a comment.