• 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

Adding Parent Category to body class

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 › Adding Parent Category to body class

This topic is: resolved

Tagged: custom body class, Parent Category

  • This topic has 10 replies, 3 voices, and was last updated 11 years, 5 months ago by Gina.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • February 17, 2015 at 1:13 pm #141125
    Gina
    Member

    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 #141332
    Gina
    Member

    So nobody has any idea how to do this???

    February 19, 2015 at 12:06 pm #141333
    Genesis Developer
    Member

    See the codex http://codex.wordpress.org/Function_Reference/body_class

    and this https://wordpress.org/support/topic/how-to-display-the-parent-category-in-the-archive


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    February 19, 2015 at 1:36 pm #141359
    RitzyThemes
    Member

    try 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 #141371
    Gina
    Member

    Thank 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 #141450
    RitzyThemes
    Member

    the post must have parent and child category.


    Get free and premium Genesis Child Themes

    February 20, 2015 at 9:15 am #141460
    Gina
    Member

    Am 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 #141465
    Genesis Developer
    Member

    You 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?


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    February 20, 2015 at 10:23 am #141467
    Genesis Developer
    Member

    Also you can see this Codex http://codex.wordpress.org/Function_Reference/get_category_parents . It is fetching the parent category.


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    February 20, 2015 at 1:06 pm #141518
    Gina
    Member

    Yes 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 #141531
    Gina
    Member

    What 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.

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

© 2026 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