When viewing a single post, the wp_list_categories
function already marks the current category of this particular post by assigning the current-cat
CSS class.
If the post is mapped to multiple categories, however, only the first category (hierarchically ordered) is marked.
How can I assign the current-cat
class to all categories of the post?
In order to assign the
current-cat
class to all categories of the current post we will extend thewp_list_categories
function by means of a dedicatedWalker
.Suppose the current setup looks as follows:
Now we add the walker (which we will set up in the next step), and a new argument
highlight
that will provide an array of the current post’s category IDs and that will be handled by our walker. Of course, we also have to populate the very array.This leads to the following:
The last step is the walker itself. Basically, we take the default
Walker_Category
class and add what’s missing. Therefore we only have to customize thestart_el
function. Directly after the$class
variable has been instantiated we add the following:And that’s it.
Altogether, this is the new walker class, which we put in our
functions.php
file:References:
get_the_category
function Codex pageWalker
class Codex pageWalker_Category
class source