How do I add classes in the wp_list_category , I know the wp_list_categories('title_li='); generates classes , but I want to add a class in the parent category link

  <li><a href="#"> link1</a> </li>
  <li><a href="#">link2 </a> </li>
  <li><a href="#">link3 </a>    <---how do I add a special class here
      <ul class="children">  
        <li><a href="#">link3 children </a> </li>
        <li><a href="#"> link3 children</a> </li>

Im planning to use a jquery UI accordion in here,
please help. thank you

-edit-- problem solved by Paul , thanks man


jquery accordion accepts an option called header that allows you to provide a selector to designate the items you want to act as the accordion headers.

$('li.categories > ul').accordion({ header: 'li.categories > ul > li' });


I have gotten this to work mainly by reverse-engineering the access-keys plugin. The code is pasted below but feel free to checkout the code for that plugin. It may help you decipher whats going on a little better.

add_filter('wp_list_categories', 'my_class_name_cats');
function my_class_name_cats($cats) {
        return preg_replace_callback('!(<li class="cat-item (cat)-item-([0-9]*)">[\s\S]*?<a([^>]*)>)!ims', 'my_class_name_finish', $cats);

add_filter('wp_list_pages', 'my_class_name_pages');
function my_class_name_pages($pages) {
    return preg_replace_callback('!(<li class="page_item (page)-item-([0-9]*)"><a([^>]*)>)!ims', 'my_class_name_finish', $pages);

function my_class_name_finish($matches){
    $id = $matches[3];
    $link = $matches[0];
    $class = $matches[4]. ' class="page-'.$id.'" ';
    $link = str_replace($matches[4], $class, $link);
    return $link;

You can change class="page-'.$id.'" to whatever you would like. Currently it sets the class to page-[id of page or category]

Add the code to your themes functions.php. If you only want it for categories you can ignore the wp_list_pages filter.

Hope this helps.


There's a filter in the works:


But in the meantime, a jquery solution might be the best fit for you, since you're loading jquery anyway.

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.