Remove line breaks in wp_list_categories()?

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