Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My code call menu:

$defaults = array(
    'theme_location'  => '',
    'menu'            => '',
    'container'       => 'div',
    'container_class' => true,
    'container_id'    => 'btn',
    'menu_class'      => true,
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '<li class="btn_bar"><img width="18" height="38" src="http://localhost/client/fzsytech/wp-content/themes/fzsytech/img/pic-btn_bar.jpg"></li>',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul>%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $defaults );

?>

And, my code when output :

<ul>
    <li id="menu-item-xx" class="blah blah">ABC</li>
    <li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li>
    <li id="menu-item-xx" class="blah blah">DEF</li>
    <li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li>

I want remove <li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li> in the last menu ?

share|improve this question
2  
then do not use the line in array –  JMau Jul 6 '13 at 19:00

3 Answers 3

Add this code function.php

function replace_last_nav_item($items, $args) {
  return substr_replace($items, '', strrpos($items, $args->after), strlen($args->after));
}
add_filter('wp_nav_menu','replace_last_nav_item',100,2);

Add this code menu

<?php
$defaults = array(
   'menu'            => 'footer-menu',
  'container'       => 'ul',
  'container_class' => '',
  'container_id'    => '',
  'menu_class'      => '',
  'menu_id'         => '',
  'echo'            => true,
  'fallback_cb'     => false,
  'before'          => '',
  'after'           => '&nbsp;&nbsp;&nbsp;|&nbsp;',
  'link_before'     => '',
  'link_after'      => '',
  'items_wrap'      => '<ul>%3$s</ul>',
  'depth'           => 0,
  'walker'          => ''
  );
  wp_nav_menu( $defaults ); ?>
share|improve this answer
1  
Please add some explanation to your code. File an edit to add all relevant info to what your code does and how it works. Also, if you add code, indent each line with 4 spaces or highlight the code in the editor and click on the {} sign above the editor to add your code in a code block –  Pieter Goosen Feb 7 at 9:54

This will replace the last occurrence of your after string.

function replace_last_nav_item($items, $args) {
  return substr_replace($items, '', strrpos($items, $args->after), strlen($args->after));
}
add_filter('wp_nav_menu','replace_last_nav_item',100,2);

Pure CSS will do this too:

.btn_bar:last-child{display:none}
share|improve this answer
    
Thank you very much. I follow your way and it was just the way my –  Nessie Jul 6 '13 at 19:59
1  
If this solved the problem please mark it as accepted. –  s_ha_dum Jul 6 '13 at 20:02
$after = '<li class="btn_bar"><img width="18" height="38" src="http://localhost/client/fzsytech/wp-content/themes/fzsytech/img/pic-btn_bar.jpg"></li>';
        $defaults = array(
                'theme_location'  => 'primary',
                'menu'            => '',
                'container'       => 'div',
                'container_class' => true,
                'container_id'    => 'btn',
                'menu_class'      => true,
                'menu_id'         => '',
                'echo'            => false,
                'fallback_cb'     => 'wp_page_menu',
                'before'          => '',
                'after'           => $after,
                'link_before'     => '',
                'link_after'      => '',
                'items_wrap'      => '<ul>%3$s</ul>',
                'depth'           => 0,
                'walker'          => ''
            );

            $menu = wp_nav_menu( $defaults );  
            echo substr($menu, 0, strripos($menu, $after)).'</ul>';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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