BrightLeaf Digital Code Snippet Library

Check out our growing library of code snippets for Gravity Forms!

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...

Code Copied!
Stay Updated On This Snippet

This entry has no reviews.