Remove line breaks in wp_list_categories()?

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.

3 Answers
3

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!

Leave a Comment