The default walker used by wp_list_categories()
(Walker_Category
) has a hook (a filter hook) named category_list_link_attributes
which you can use to add custom title
and class
(as well as other attributes like data-xxx
) to the anchor/a
tag (<a>
) in the HTML list generated via wp_list_categories()
.
So for example, you can do something like:
add_filter( 'category_list_link_attributes', 'my_category_list_link_attributes', 10, 2 );
function my_category_list_link_attributes( $atts, $category ) {
// Set the title to the category description if it's available. Else, use the name.
$atts['title'] = $category->description ? $category->description : $category->name;
// Set a custom class.
$atts['class'] = 'custom-class category-' . $category->slug;
// $atts['class'] .= ' custom-class'; // or append a new class
// $atts['class'] = 'custom-class ' . $atts['class']; // or maybe prepend it
return $atts;
}