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

Community Forums Forums Design Tips and Tricks Code snippets – add body class to category or to page template

This topic is: not resolved

This topic contains 4 replies, has 3 voices, and was last updated by  Jen Baumann 1 year, 9 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2654

    Dorian Speed
    Participant
    Post count: 140

    (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!

    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    • This topic was modified 1 year, 10 months ago by  Dorian Speed.
    #2674

    Jen Baumann
    Keymaster
    Post count: 416

    I do this quite frequently for certain client templates.

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


    If you need technical support for your theme please file a ticket.

    The forums are community based. Staff only monitors the forum for issues relating to the forum itself and to redirect users to where they need to go.

    #2682

    Dorian Speed
    Participant
    Post count: 140

    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!

    #6608

    zubird
    Participant

    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!

     

    #7564

    Jen Baumann
    Keymaster
    Post count: 416

    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()


    If you need technical support for your theme please file a ticket.

    The forums are community based. Staff only monitors the forum for issues relating to the forum itself and to redirect users to where they need to go.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.