Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding Parent Category to body class
Tagged: custom body class, Parent Category
- This topic has 10 replies, 3 voices, and was last updated 9 years, 7 months ago by Gina.
-
AuthorPosts
-
February 17, 2015 at 1:13 pm #141125GinaMember
I am trying to add parent category name to the body class of archives and single-posts. I have gotten the category name into the body class of both but not the parent category name. Everything I find online about this does not seem to work. I'm using Genesis framework with Going Green Pro child theme for what it's worth. Any help would be very much appreciated!
http://plaqueminesparish.com/wp/category/homeland-security/hsep-in-depth/February 19, 2015 at 12:02 pm #141332GinaMemberSo nobody has any idea how to do this???
February 19, 2015 at 12:06 pm #141333Genesis DeveloperMemberFebruary 19, 2015 at 1:36 pm #141359RitzyThemesMembertry following code in your functions.php
function add_parent_category_body( $classes ) { global $post; $categories = get_the_category( $post->ID ); $classes[] = $categories[0]->slug; return $classes; } add_filter( 'body_class', 'add_parent_category_body' );
NOTE: Backup before making any changes in functions.php
Get free and premium Genesis Child Themes
February 19, 2015 at 3:32 pm #141371GinaMemberThank you RitzyThemes. I tried add that code but I still don't get the parent category in the body tag for some reason.
February 20, 2015 at 8:12 am #141450RitzyThemesMemberFebruary 20, 2015 at 9:15 am #141460GinaMemberAm I missing something? This is my example: http://plaqueminesparish.com/wp/category/homeland-security/hsep-in-depth/
hsep-in-depth is the child.
homeland-security is the parent.
Or am I not understanding that correctly?Thank you for your help!!!
February 20, 2015 at 10:20 am #141465Genesis DeveloperMemberYou are wanting to add the "homeland-security" into the body class Correct? If there have sub-sub category then it will add two parent category?
February 20, 2015 at 10:23 am #141467Genesis DeveloperMemberAlso you can see this Codex http://codex.wordpress.org/Function_Reference/get_category_parents . It is fetching the parent category.
February 20, 2015 at 1:06 pm #141518GinaMemberYes I am wanting to add "homeland-security" to the body class. Not sure what you are saying about sub-sub category. Sorry. I read over that codex but I'm just not sure what I'm supposed to do with that info. Oh well. I guess I need to find a different solution. Thank you for trying!
February 20, 2015 at 1:31 pm #141531GinaMemberWhat I am actually trying to do is style everything with the homeland-security parent category the same way...different nav colors and different sidebar content. There are other ways I can do it but I just thought using the parent category body class would be the easiest way.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.