1
<ul id="menu-primary">
    <li></li>
    <li></li>
    <li></li><li class="stretcher"></li> /* add adjacent to the last menu item */
</ul>

I need to add <li class="stretcher"></li> adjacent to the last menu item exactly as shown, to the menu with id="menu-primary".

(The reason is to remove the whitespace generated in some browsers. Similar to the first answer in this question: Fluid width with equally spaced DIVs)

Community
  • 1
  • 1
Sunny
  • 2,232
  • 7
  • 24
  • 36

2 Answers2

2

I'd add it with a filter:

add_filter('wp_nav_menu_items', 'add_stretcher', 10, 2);
function add_stretcher($items, $args) {
  if ($args->theme_location == 'primary') {
    $items .= '<li class="stretcher"></li>';
  }
  return $items;
}
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • It's not working. Don't see anything added to the menu. It's not a primary menu registered to the theme, if that makes a difference, I just gave the menu name "primary". – Sunny Sep 06 '13 at 15:01
  • Temporarily, try losing the "if", and just have `$items .= '
  • '; return $items;` as the function body. I only added that so the stretcher li wouldn't be added to every menu. – Matt Gibson Sep 06 '13 at 16:20
  • Now I see it, but it's not adjacent to the last menu item (as shown above) in the markup. – Sunny Sep 06 '13 at 16:31
  • Interesting -- perhaps try `$items = rtrim($items) . '
  • ';` instead of `$items .= '
  • ';` to get rid of any whitespace/linefeeds etc. on the existing items? – Matt Gibson Sep 06 '13 at 16:41
  • Now all that's left is to target this specific menu, but I could live without that. – Sunny Sep 06 '13 at 17:06