Tag Archive | menu

WordPress: Adding a class to first and last menu items

function add_first_and_last($output) {
$output = preg_replace('/class="menu-item/', 'class="first-menu-item menu-item', $output, 1);
$output = substr_replace($output, 'class="last-menu-item menu-item', strripos($output, 'class="menu-item'), strlen('class="menu-item')); return $output;
}
add_filter('wp_nav_menu', 'add_first_and_last');