Community Forums › Forums › Archived Forums › Design Tips and Tricks › Code snippets – add body class to category or to page template
Tagged: body classes, page template, style category, theme colors
- This topic has 4 replies, 3 voices, and was last updated 11 years, 3 months ago by Jen Baumann.
-
AuthorPosts
-
November 30, 2012 at 4:15 pm #2654Dorian SpeedMember
(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 #2674Jen BaumannParticipantI 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 #2682Dorian SpeedMemberI 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 #6608zubirdMemberHi 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 #7564Jen BaumannParticipantRemoving 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()
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.