• 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

Code snippets – add body class to category or to page template

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 › Code snippets – add body class to category or to page template

This topic is: not resolved

Tagged: body classes, page template, style category, theme colors

  • This topic has 4 replies, 3 voices, and was last updated 12 years, 6 months ago by Jen Baumann.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • November 30, 2012 at 4:15 pm #2654
    Dorian Speed
    Member

    (Disclaimer: anything I post as "what worked for me" may not be the best way to do this, but I was happy to figure out A solution and am posting this in case it helps someone else.)

    For a site I'm working on, I wanted to be able to switch between color schemes on a per-post or page-template basis. This allowed me to avoid having to add a bunch of extra body class stuff to style.css.

    To specify that a post in a given category would use a particular color scheme - here, the category "Game Show":

    /**Assign custom body class to Game Show category */
    add_action( 'body_class', 'gameshow_bodyclass');
    function gameshow_bodyclass( $classes ) {
    if ( in_category( 'game-show-events' ))
    $classes[] = 'legacy-silver';
    return $classes;
    }

    To create a page template that utilizes an existing color scheme within the theme:

    Create a page template (here, it was page-game.php) with this as the content of the file:

    <!--?php /*
    Template Name: Game Show
    *

    /** Add custom body class to the head */
    add_filter( 'body_class', 'add_body_class' );
    function add_body_class( $classes ) {
    $classes[] = 'legacy-silver';
    return $classes;
    }

    genesis();
    Upload that file via FTP to the theme folder.


    Bringing websites Up to Speed
    Firebug will light the way to understanding the secrets of the Internet!

    November 30, 2012 at 4:55 pm #2674
    Jen Baumann
    Participant

    I do this quite frequently for certain client templates.

    Another option is to use the body class field under Layout settings.

    November 30, 2012 at 5:09 pm #2682
    Dorian Speed
    Member

    I NEVER NOTICED THAT WAS THERE.

    This changes everything.

    Well, not everything, but that is really cool and useful. I wish this new forum had a "sheepishly shuffling off the page" emoticon I could deploy here.

    I guess my solution would work if you have a client who doesn't want to have to remember to use the Layout settings when composing a post.


    Bringing websites Up to Speed
    Firebug will light the way to understanding the secrets of the Internet!

    December 21, 2012 at 4:47 am #6608
    zubird
    Member

    Hi Ladies,

    I am trying to retain the chosen color (green) for my home page but change the color elsewhere on my site for certain pages and posts. I am able to add the new color class to the pages and posts, but thus far have been unsuccessful in removing the streamline-green class from anything. Among other things, here is what I tried:

    /** Add custom body class to the head for certain pages or categories */
    remove_action( 'body_class', 'nogreen_class' );
    function nogreen_class( $classes ) {
    if ( in_category( '4' ) OR ( is_page( 164 || 351 || 240 || 191 || 243 || 68 )))
    $classes[] = 'streamline-green';
    return $classes;
    }

    /** Add custom body class to the head for certain pages or categories */
    add_action( 'body_class', 'add_orange_class' );
    function add_orange_class( $classes ) {
    if ( in_category( '4' ) OR ( is_page( 164 || 351 || 240 || 191 || 243 || 68 )))
    $classes[] = 'streamline-orange';
    return $classes;
    }

    Seems that it should work but does not. Any clues?

    I do prefer to use a function so we don't have to worry about page level tweaking.

    I appreciate any help.

    Thank you!

     

    December 26, 2012 at 3:16 pm #7564
    Jen Baumann
    Participant

    Removing theme level body classes would be different than that. I think the easier thing to do is leave the color as the default color setting, then add body classes as appropriate. The conditional for home is is_home()

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 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