2

If I pass in 'style' => 'none' as an argument to wp_list_categories, the <li> tags are removed. That's great, but <br> tags are injected. How can I remove them?

4

The new separator attribute of wp_list_categories()

I think you are looking for the new separator attribute, that will be introduced here in WordPress 4.4 that's just around the corner. I located the trac ticket here #9025.

Then you can use:

$args = [
    'style'     => 'none',
    'separator' => '', // <-- Removes the default one
];

wp_list_categories( $args );

where by default it's 'seperator' => '<br />';

Example:

We get:

<a href="http://example.tld/category/red/" >Red</a>
<a href="http://example.tld/category/green/" >Green</a>
<a href="http://example.tld/category/blue/" >Blue</a>

instead of

<a href="http://example.tld/category/red/" >Red</a><br />
<a href="http://example.tld/category/green/" >Green</a><br />
<a href="http://example.tld/category/blue/" >Blue</a><br />
3
  • You are quite busy with 4.4. Just been to busy with other stuff to really peak into 4.4, seems I need to get my priorities straight here, falling behind, ;-) Nov 20 '15 at 10:34
  • 2
    Lots of interesting stuff and improvements in 4.4 ;-) I just filed a ticket #34751 to improve the cluttered default arguments in wp_list_categories() for better readability. @PieterGoosen
    – birgire
    Nov 20 '15 at 10:56
  • That would be a great. Some code in core are really bad when it comes to readability. Hope your ticket makes core. ;-) Nov 20 '15 at 11:03
1

Try something like this:

$args = array( 
    'echo' => 0,
    // your other parameters
);
echo str_replace( "<br>", "", wp_list_categories( $args ) );
1
  • 1
    hmmm, I have no idea why people downvoted this. Nov 20 '15 at 4:05
1

Sadly i can not comment yet but James Jones is right in wp-includes/category-template.php @1103 you'll see that if the style parameter is not set it will produce a <br /> at the end of each element.

the only thing that is not right is that it should be

str_replace( "<br />", "", wp_list_categories( $args ) );

alsoe keep in mind to set the parameter echo to false otherwise it will output the html right away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.