Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<ul>
  <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>
      </li>
 </li>
 </ul> 

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

-edit-- problem solved by Paul , thanks man

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

There's a filter in the works:

http://core.trac.wordpress.org/ticket/16497

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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