• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Custom body class for specific categories and archives

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom body class for specific categories and archives

This topic is: resolved

Tagged: custom category body class

  • This topic has 2 replies, 2 voices, and was last updated 10 years, 2 months ago by Gina.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • February 23, 2015 at 10:53 pm #141986
    Gina
    Member

    I have this code working for the most part to add a custom body class to specific categories:

    /* add a custom body class */ 
    add_action( 'body_class', 'ilwp_add_my_bodyclass'); 
    function ilwp_add_my_bodyclass( $classes ) { 
     if ( is_category( array(parish-plan,residential-emergency-planning,business-emergency-planning,evacuation-guidelines) ) ) 
       $classes[] = 'hsep'; 
     return $classes; 
    }

    However I need to add something to the effective of else...don't add that custom body_class to any other category but I cannot figure out the proper syntax. I thought this code would add the hsep body class to only those categories but unfortunately it has also been added to random other categories so I'm guessing I need an else statement.

    http://plaqueminesparish.com/wp/homeland-security-emergency-preparedness/
    February 24, 2015 at 7:17 am #142007
    Victor Font
    Moderator

    is_category tests whether you are displaying a category archive or not. If the page isn't a category archive, it will always return false. Looking at your menu in source code, I only see four category archive pages that are being displayed. These pages are Latest News, Media, Parish Plan, and Evacuation Guidelines. All four have the hsep class added at the end of the body class list. This means that is_category is returning true for all categories and is not honoring the array list. The probable reason the array is not being honored is because you haven't properly delimited your strings with quotes.

    /* add a custom body class */ 
    add_action( 'body_class', 'ilwp_add_my_bodyclass'); 
    function ilwp_add_my_bodyclass( $classes ) { 
     if ( is_category( array('parish-plan','residential-emergency-planning','business-emergency-planning','evacuation-guidelines') ) ) 
       $classes[] = 'hsep'; 
     return $classes; 
    }

    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    February 24, 2015 at 9:11 am #142021
    Gina
    Member

    Thank you so much! I have definitely looked at this too long and was not seeing the obvious. That took care of my problem.

    My problem was, Latest News & Upcoming Events were NOT supposed to have the hsep body tag.

    I really appreciate your time looking at this!!!!!!

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble