When using wp_nav_menu how do I make it so 'after' => does not show on the last element of the list?


You can hide it with CSS. For example, if your after looks like:

<?php wp_nav_menu(array('after' => '<span>|</span>')); ?>

Then your css:

.menu-item-num span { display: none; }
$('#menu-top-menu-1').children().last().each( function(){
    var endHtml = $(this).html().replace('<text you want to replace>','<text to replace it with>');         
  • Can't edit, but also need to change #menu-top-menu-1 to your menu ID
    – Jeff
    Mar 1 '11 at 20:44

While this can be done with JS, it might be more tidy to use wp_nav_menu filter, through which final output of menu is passed before returning/echoing.

  • 2
    You should expound on this Feb 28 '13 at 4:00

Found this on another Answer

https://wordpress.stackexchange.com/a/105562/105 , but this question came up first when searching; so cross linking for future searchers

function replace_last_nav_item($items, $args) {
  return substr_replace($items, '', strrpos($items, $args->after), strlen($args->after));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.