Take the 2-minute tour ×

I am trying to output a list of categories per an article. I am using the following code,

<?php wp_list_categories('child_of=270&style=none'); ?>

(string) Style to display the categories list in. A value of list displays the categories as list items while none generates no special display method (the list items are separated by <br> tags).

How do I remove the stupid <br /> after each output?

Frustrating how this code

<?php the_category(', '); ?>

Will output the links without a line break...

Any help would be greatly appreciated.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I came to a solution for this by reviewing the Wordpress Codex. The trick is to turn off the automatic echo of wp_list_categories, and then use str_replace(). My example follows:

<?php $variable = wp_list_categories('child_of=270&style=none&echo=0'); ?>
<?php $variable = str_replace('<br />', '', $variable); ?>
<?php echo $variable; ?>

The only real change in your query is the inclusion of "echo=0" which will return the information in a variable rather than just displaying it.

The real work is done by str_replace() which looks for any <br /> tags and removes them from the string. Then just echo out the updated string and no more breaks!

share|improve this answer
what an elegant, and effective solution. i am quickly learning how viable str_replace is becoming with WP. TY! –  HollerTrain Dec 1 '10 at 20:45
add comment

Thanks for the answer above, I found a solution that allows for even more customisation of the output of wp_list_categories. I'm using this for showing all terms in a taxonomy.

        $args = array(
          'taxonomy'     => 'place',
          'orderby'      => 'name',
          'style'        => 'list',
          'show_count'   => 0,
          'pad_counts'   => 0,
          'hierarchical' => 1,
          'title_li'     => '',
          'exclude'      => '28',
          'echo'         => '0'


As opposed to the example above which requires 'style' => 'none', I'm using 'style' => 'list', outputting an unordered list with <li> and <ul> tags but no <br /> tags.

Then the PHP command strip_tags selectively strips all html tags. This would of course also work for stripping the <br /> tag when using 'style' => 'none'.

    <?php $variable = wp_list_categories($args); ?>
    <?php $variable = strip_tags( $variable, '<a>' ); ?>
    <?php echo $variable; ?>

The second parameter in strip_tags are the allowable tags, i.e. tags that are kept in the code. So it's keeping the <a> tag to preserve all links, and bingo!

Now the deluxe version allows, for example, to style child terms differently to their parents:

    <?php $variable = strip_tags( $variable, '<a><ul>' ); ?>
    <?php $variable = str_replace('ul', 'span', $variable); ?>
    <?php echo $variable; ?>

Firstly this keep the <a> tag and the <ul> tag which always has the class "children". Then it replaces 'ul' with 'span' and the tag will read <span class="children">. Now all list tags are gone and you're free to add a CSS style for the class "children".

share|improve this answer
Why not extend the category walker and produce the output you need, instead of doing expensive string matching and replacing? –  onetrickpony Apr 11 '12 at 21:30
add comment

Your Answer


