Skip Navigation

[Resolved] add taxonomy name as body class

This thread is resolved. Here is a description of the problem and solution.

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Steve 3 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#2197179

Hi on this page
hidden link
Im trying to figure out a way to style the buttons that corresponds to an actively selected category so that viewers see what the active category is I was wondering if there might be a way to add the category slug as a body class then i could target the corresponding button with some css like this

body.CATNAME a#SAMECATNAME{
backgroundcolor:red;
}

Or perhaps theres another way you could suggest.

Thanks

#2197553

Hello and thank you for contacting Toolset support.

Well, there is no built-in feature in Toolset for this case. But you can do it with regular WordPress functions. Something like:

add_filter( 'body_class', 'custom_taxonomy_slug_to_body_class', 99, 1 );
function custom_taxonomy_slug_to_body_class( $classes ){
    if( is_tax('internships') ){
        $classes[] = get_queried_object()->slug;
    }
    return $classes;
}

read more about the user functions here:
- https://developer.wordpress.org/reference/functions/is_tax/
- https://developer.wordpress.org/reference/functions/get_queried_object/
- https://developer.wordpress.org/reference/hooks/body_class/

Please note that this code assumes that this is about a custom taxonomy 'internships'. If you are, actually, using the default categories and renaming their slug to 'interniships', you should use is_category() instead of is_tax('internships')

I hope this helps. Let me know if you have any questions.

#2199273

Thanks so much Jamal really appreciate that