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.

I'm trying to remove the last separator (normally a <br/> tag but I changed it to "//") from the last link from wp_list_categories.

Basically I want this:

Category 1 // Category 2 // Category 3 //

to look like this:

Category 1 // Category 2 // Category 3

Here's the current code I'm using:

<?php
    $cat_array = array();
    $args = array(
        'author' => get_the_author_meta('id'),
        'showposts' => -1,
        'caller_get_posts' => 1
    );
    $author_posts = get_posts($args);
    if ( $author_posts ) {
        foreach ($author_posts as $author_post ) {
            foreach(get_the_category($author_post->ID) as $category) {
                $cat_array[$category->term_id] =  $category->term_id;
            }
        }
    }

    $cat_ids = implode(',', $cat_array);
    echo strtr(wp_list_categories('include='.$cat_ids.'&title_li=&style=none&echo=0'),array('<br />'=>' // '));
?>
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Change the last line to this:

$output = strtr( wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ), array( '<br />' => ' // ' ) );
echo preg_replace( '@\s//\s\n$@', '', $output );
share|improve this answer
add comment

You got a lotta string processing going on there. You might be better off outputting the results as a list

wp_list_categories('include='.$cat_ids.'&title_li=');

and styling it with css:

li.cat-item { list-style-type: none; display: inline; }
li.cat-item:before { content: "&#32;&#47;&#47;&#32;"; }
li.cat-item:first-child:before { content: none; }

Just another alternative to consider...

share|improve this answer
    
Similar example here: remove last separator –  Geert May 3 '11 at 6:13
add comment

Another option, explode -> pop -> implode...

$output = explode( '<br />', wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ) );
array_pop($output);
echo implode(' // ',$output);
share|improve this answer
add comment

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.