Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding a custom body class to several category archives
Tagged: category archive, custom body class
- This topic has 8 replies, 2 voices, and was last updated 9 years, 7 months ago by Gina.
-
AuthorPosts
-
February 23, 2015 at 11:45 am #141886GinaMember
I cannot figure out how to add a custom body class to more than one category archive. I have a website that has two different styles to the navigation...one part gold, the other blue with a secondary nav. So all of the gold portion is styled and working fine. All of the blue portion is styled correctly except for the category archives. I can get one category archive navigation styled correctly. I also found the following code that will allow me to add a custom body class (same one I'm using for pages in the blue section) to ONE category archive but for some reason I am unable to add it to more than one.
/* add a custom body class */
add_action( 'body_class', 'ilwp_add_my_bodyclass');
function ilwp_add_my_bodyclass( $classes ) {
if ( is_category( 'parish-plan' ))
$classes[] = 'hsep';
return $classes;
}Anyone know how I can add the body class hsep to more than one category archive?
http://plaqueminesparish.com/wp/category/homeland-security/parish-plan/February 23, 2015 at 11:52 am #141887GinaMemberI finally found the answer to this question! I've been trying to figure this out for days 🙂
For multiple categories the below works:/* 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) ) ) { echo 'hi'; }
$classes[] = 'hsep';
return $classes;
}You can enter the category id instead if you don't have the parent category name inside the body tag.
February 23, 2015 at 2:12 pm #141907GinaMemberSo I almost have this resolved 🙂
I added the add action as stated above and it worked EXCEPT it added it to two categories that I did not include and do not want it to be added to: latest-news and upcoming-events.
I'm not sure why it did that. I tried adding an else if but I don't have all of the syntax correct. Can someone please help!!!!
February 23, 2015 at 3:16 pm #141917Brad DaltonParticipantHard to test the code you pasted as its not wrapped in pre or code tags so it breaks when added to the functions file.
Also, where did you get the code from? Please link to the source.
February 23, 2015 at 6:44 pm #141948GinaMemberI have replied to this twice. Are you getting them? I don't see them on this page so wasn't sure 🙂
February 23, 2015 at 6:46 pm #141949GinaMemberWell since that one displays I will try this again...
Thank you for your reply! This is where I found the code: http://ilikewp.com/how-to-add-a-custom-body-class/
Here is the code as requested sorry about that:
/* 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; }
February 23, 2015 at 6:47 pm #141950GinaMemberHere is the code as requested:
/* 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; }
The link where I got it from will not post for some reason. It was in an article titled "How To Add a Custom Body Class" on the I Like WordPress blog.
February 23, 2015 at 6:49 pm #141954GinaMemberI got it from ilikewp.com/how-to-add-a-custom-body-class
February 24, 2015 at 9:14 am #142022GinaMemberSo it's been pointed out that I don't have the category names in quotes. Once I did that it fixed the problem.
/* 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; }
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.