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'); ?>

Read More

(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.

Related posts

Leave a Reply

3 comments

  1. 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!

  2. 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.

       <?php
            $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 = wp_list_categories($args); ?>
        <?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”.

  3. The most simple way to get the links in line is to output with li elements so don’t set style to “none”.

    Then you can just set display: inline; on “li” elements in your container.

    much cleaner that way