Archive for November, 2010

WordPress wp_nav_menu Seperator And First Last Classes

November 10th, 2010

Using WordPress wp_nav_menu function you can display navigation menus on your site nicely.  You can even add the separators between each navigation menu.  It can be done by passing these arguments either “link_before, link_after” or “before, after” to your wp_nav_menu funtion.  However, you might face the problem with adding separators between each navigation menu.

Let’s say you would like to add “|” between navigation menu links but do not want to show it after the last menu.

e.g.
Home | About Us | FAQs | Blog | <– you do not want this

So far wp_nav_menu function does not add any additional classes to the first and last of the navigation menus.  But don’t  worry to much about that because there is a trick to fix it.  Just copy and paste below code to your functions.php

Put this into your functions.php

function nav_menu_first_last( $items ) { $position = strrpos($items, 'class="menu-item', -1);<br />
$items=substr_replace($items, 'menu-item-last ', $position+7, 0);<br />
$position = strpos($items, 'class="menu-item');<br />
$items=substr_replace($items, 'menu-item-first ', $position+7, 0);<br />
return $items;<br />
}<br />
add_filter( 'wp_nav_menu_items', 'nav_menu_first_last' );

Put this into your style.css or any css file you using.

.menu-item-last {<br />
display: none;<br />
}