BrightLeaf Digital Code Snippet Library
Check out our growing library of code snippets for Gravity Forms!
Eleven of these snippets register their own shortcodes. Every tag and attribute is in our Gravity Forms shortcodes reference. Need a Gravity Forms or GravityView shortcode built for you? Try the free shortcode builder.
Add Category to Body Class
Goal
Adds category slugs as CSS classes to the body tag on single posts and category archives, enabling category-specific styling without custom templates.
Configuration
- None - works out of the box
- Optional: Add CSS rules targeting
.category-{slug}in your theme stylesheet
Usage
- Access in CSS as:
body.category-news { /* styles * / } - Example output on single post in "News" category:
<body class="single postid-123 category-news">
Notes
- Only applies to single posts and category archives
- Multiple categories will add multiple classes
- Uses category slug (not name), e.g., "category-web-design" not "category-Web Design"
Install Using Code Snippets
This code is best installed directly on your site using the Code Snippets plugin.
Check Out the Code
Loading...
Stay Updated On This Snippet
Review this entry
You must be logged in to post a comment.