remove last separator?

using;

$args = array( 'menu' => 'mainmenu', 'container' => 'ul', 'menu_class' => 'span-10 clearfix', 'after'=>'/');
wp_nav_menu($args);

which gives me;
london/munich/artists/fairs & events/publications/news/contact/

Read More

but using the args (or similar) can i remove the / from contact?

any help welcome!

Related posts

Leave a Reply

4 comments

  1. Here is a completely different approach to the problem. Since the slashes may be considered to be presentational, they should not go into the HTML. Users without CSS just get to see a regular list.

    You could then use CSS to restyle the list and add the slashes in between.

    #nav li { display:inline; }
    #nav li:before { content:' / '; }
    #nav li:first-child:before { content:''; }
    

    Check it out here.

    Note: this will not work in IE7 and older. IE7 does support :first-child but :before only works from IE8.

  2. First +1 for using blueprint css 🙂

    Second: Yes, separators are just presentational and shouldn’t be part of the menu (screenreader using people would be confused). So here you get a simple function to add separator-css-classes or any other additional classes whereever you need them. Just modify the three vars to your needs.

    And now here’s the function that let you exactly target a single nav menu item in a specific location within a specific menu. You just have to modify the first three vars $menu_location, $menu_name, & $menu_item to your needs.

    function wpse15844_nav_top_classes( $classes, $item ) 
    {
        // EDIT HERE: This is the nav menu location slug
        $menu_location = 'top';
        // EDIT HERE: This is the nav menu name you entered in the admin-UI > Appearance > Menus (Add menu)
        $menu_name = 'Topnav';
    
        // Abort if we're not with the named menu
        if ( is_nav_menu( $menu_name ) !== true ) 
            return;
    
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_location ] ) )
        {
            $locations = get_nav_menu_locations();
    
            $menu = wp_get_nav_menu_object( $locations[ $menu_location ] );
            $menu_items = wp_get_nav_menu_items( $menu->term_id );
        }
    
        // Abort if we're not in the specified location
        else 
            return;
    
        // EDIT HERE: Enter the nr. of the menu item. Currently we're adding to the last one.
        $menu_item = intval( count( $menu_items ) );
    
        if ( $item->menu_order == $menu_item )
        {
            $classes[] = 'span-10 clearfix';
        }
        else 
        {
            $classes[] = 'span-10 clearfix separator';
        }
    
        return $classes;
    }
    add_filter( 'nav_menu_css_class', 'wpse15844_nav_top_classes', 10, 2 );
    

    You can then style your separator as you need it from inside your css file.

  3. If you can move the / to before the links ( 'before' => '/' instead of 'after' => '/' ), that would make it much easier to filter out; you could do it like this:

    function wpse15844_nav_separator_filter( $output, $item, $depth, $args ){
        global $wpse15844_nav_separators;
        if( !isset( $wpse15844_nav_separators ) )
            $wpse15844_nav_separators = array();
        if( isset( $wpse15844_nav_separators[$depth] ) && $wpse15844_nav_separators[$depth] )
            return $output;
        $wpse15844_nav_separators[$depth] = true;
        return substr( $output, strlen( $args->before ) );
    }
    
    add_filter( 'walker_nav_menu_start_el', 'wpse15844_nav_separator_filter', 10, 4 );
    
  4. And this is how you should be able to add the / after (even if not recommended). For some reason this doesn’t work as expected, even if i get the right $args when i print_r it (see #-uncommented code). It adds to the first (in my testcase) instead of the last element.

    @John P Bloch @Geert – Could you pls give this a try and tell me if this works wrong for you too? Thanks!

    function wpse15844_nav_top_args( $output, $item, $depth, $args ) 
    {
        // EDIT HERE: This is the nav menu location slug.
        $menu_location = 'top';
        // EDIT HERE: This is the nav menu name you entered in the admin-UI > Appearance > Menus (Add menu).
        $menu_name = 'Topnav';
    
        // Abort if we're not with the named menu
        if ( is_nav_menu( $menu_name ) !== true ) 
            return;
    
        // You won't need the following if statement if you're not targeting the last menu item.
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_location ] ) )
        {
            $locations = get_nav_menu_locations();
    
            $menu = wp_get_nav_menu_object( $locations[ $menu_location ] );
            $menu_items = wp_get_nav_menu_items( $menu->term_id );
        }
    
        // Abort if we're not in the specified location - If you're not searching for the last item, you won't need this one too.
        else 
            return;
    
        // EDIT HERE: Enter the nr. of the menu item in the second line inside eg. intval( 3 ). Currently we're adding to the last one.
        $menu_item = count( $menu_items );
        $menu_item = intval( $menu_item );
    
        // Add the class
        if ( $item->menu_order !== $menu_item )
        {
            $args->after = '/';
            # echo '<pre>'; print_r( $args ); echo '</pre>';
        }
        else 
        {
            $args->after = '';
            # echo '<pre>'; print_r( $args ); echo '</pre>';
        }
    
        return $output;
    }
    add_filter( 'walker_nav_menu_start_el', 'wpse15844_nav_top_args', 10, 4 );