Add a Class to Parent Categories Using wp_list_categories in WordPress

There was a quesiton in the WordPress IRC channel just a little while ago asking if there was a way to add a class to the li tag of parent categories generated by the wp_list_categories function.

The first idea that came to mind was using javascript, but a PHP solution will work without the requirement of running javascript on the client machine.

The solution is to read the output of wp_list_comments into a variable, split/explode the string into an array, loop through the array checking if the next element is <ul class=’children’>, if the next element is the child ul then add a class to the current li and echo, otherwise just echo the element. Sounds easy right? The code is actually easier to write than describing it using words. As with all of my WordPress code snippets relating to a theme change I have used the WordPress default theme. The following code goes in sidebar.php and replaces the current wp_list_categories function.

<?php
$categories = wp_list_categories('show_count=1&title_li=<h2>Categories</h2>&echo=0');
$category_array = preg_split('/\n/', $categories);
$count = count($category_array);
$i = 0;
while ( $i < $count ) {
        if ( preg_match('/<ul class=(\'|")children(\'|")/i', $category_array[$i+1]) ) {
                echo preg_replace('/<li class=(\'|")(.+)(\'|")>/i', '<li class=$1parent $2$3>', $category_array[$i]) . "\n";
        } else {
                echo $category_array[$i] . "\n";
        }
        $i++;
}
?>

Enjoy!

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in Code, PHP, Snippet, WordPress. Bookmark the permalink.

5 Responses to Add a Class to Parent Categories Using wp_list_categories in WordPress

  1. Thank you, I was looking for the code cause I was experiencing same trouble with adding classes to categories. Good luck in your job, you really helped me out

  2. Hleb says:

    Thank you very much! Regular expressions is the best and easiest way in that case.

  3. Ccaraba says:

    It takes almost an hour looking for this wp code.. thanks..
    It works like a charm..

  4. PaulH says:

    Thank you! Exactly what I was looking for!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>