Title: Dynamic body classes
Last modified: August 19, 2016

---

# Dynamic body classes

 *  [pgapepper](https://wordpress.org/support/users/pgapepper/)
 * (@pgapepper)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/)
 * I’m frustrated beyond all belief right now. I’ve been trying to apply different
   CSS to the body of different page groups and I’m losing my mind. Forgive me, 
   but I’m well versed in HTML & CSS, but php has never been a strength of mine.
   This project is a freebie for my church.
 * I need to apply a different style to specific pages (not posts) and their child
   subpages. I’ve tried using built-in body classes, such as page-child parent-pageid-(
   id) in CSS and just can’t get it to work and I’ve tried darn near every method
   listed on Perishablepress’s ([http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/](http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/))
   site.
 * What I’d like is to style a parent (and children) with a body class of .church,
   another parent and their children with .school, and another parent/children with.
   daycare. All of the forums and blogs out there say it is sooo simple with dynamic
   body classes (which I believe), but I just can’t seem to get it figured out. 
   Any input, thoughts, suggestions are much appreciated. Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651568)
 * a link would help to illustrate which body classes are already present – if you
   use `body_class()`
 * otherwise, build your own body classes:
    – find the parent page of the child 
   pages, or the parent themselves; – either use the page slug directly, or make
   a css class depending on the page name or id;
 * example, assuming that `body_class()` is not used:
    in header.php; instead of`
   <body>`
 * insert:
 *     ```
       <?php if($post->post_parent)
       { $top_page = get_post($post->post_parent); }
       else
       { $top_page = $post; }
       $top_page_name = $top_page->post_name;
       ?>
       <body<?php if($top_page_slug) { echo ' class="' . $top_page_name . '"'; ?>>
       ```
   
 * only for one generation parent-child (no grandchildren);
    that will work if your
   parent pages are called ‘church’, ‘school’ and ‘daycare’ (with or without capital
   letters);
 *  Thread Starter [pgapepper](https://wordpress.org/support/users/pgapepper/)
 * (@pgapepper)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651600)
 * Thanks for the quick response, alcymyth. The site is currently located at trinity.
   versiostudios.com while it’s being built and sandboxed. I should have included
   the url (sorry) in the original post. I’ll try and implement your solution, but
   what would happen if the .church class needs to apply to all pages, except those
   specified as parent/child of posts 30 or 32? Grandchildren will exist also. I
   would think it’d be a simple if / elseif / else, right?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651618)
 * there is a typo in the above – correction here:
 *     ```
       <?php if($post->post_parent)
       { $top_page = get_post($post->post_parent); }
       else
       { $top_page = $post; }
       $top_page_name = $top_page->post_name;
       ?>
       <body<?php if($top_page_name) { echo ' class="' . $top_page_name . '"'; ?>>
       ```
   
 * to take care of the grandchildren, see: [http://cssglobe.com/post/5812/wordpress-find-pages-top-level-parent-id/](http://cssglobe.com/post/5812/wordpress-find-pages-top-level-parent-id/)
 * if `.church` is the default body class (for pages and for all other templates,
   such as archives (?)) –
    then something like this might work: revised code (should
   take care of grandchildren and the default class):
 *     ```
       <?php if($post->post_parent)
       {  $ancestors=get_post_ancestors($post->ID);
          $root=count($ancestors)-1;
          $parent = $ancestors[$root];
          $top_page = get_post($parent); }
       else
       { $top_page = $post; }
       $top_page_name = $top_page->post_name;
       if ( $top_page_name != 'school' && $top_page_name != 'daycare' ) $top_page_name = 'church';
       ?>
       <body<?php if($top_page_name) { echo ' class="' . $top_page_name . '"'; ?>>
       ```
   
 *  Thread Starter [pgapepper](https://wordpress.org/support/users/pgapepper/)
 * (@pgapepper)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651643)
 * Leaving the following in:
 *     ```
       if (apply_filters('thematic_show_bodyclass',TRUE)) {
           Creating the body class
           ?>
       ```
   
 *  (or taking it out – either way it makes no difference)
    and replacing `<body
   class="<?php thematic_body_class() ?>">` with
 *     ```
       <?php if($post->post_parent)
       {  $ancestors=get_post_ancestors($post->ID);
          $root=count($ancestors)-1;
          $parent = $ancestors[$root];
          $top_page = get_post($parent); }
       else
       { $top_page = $post; }
       $top_page_name = $top_page->post_name;
       if ( $top_page_name != 'school' && $top_page_name != 'early-childhood-education' ) $top_page_name = 'trinity';
       ?>
       <body<?php if($top_page_name) { echo ' class="' . $top_page_name . '"'; ?>>
       ```
   
 * results is the following error:
 * > Parse error: syntax error, unexpected $end in ……/wp-content/themes/thematic/
   > header.php on line 96
 * As an aside, I am oviously using the thematic theme as a framework (I also changed
   the naming conventions of the classes to match page slugs already in place). 
   Thanks again for all of your help.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651646)
 * my bad – 🙁
    i think a missing closing } of the if statement in the `<body` line
   is the cause for the parse error (corrected below):
 *     ```
       <?php if($post->post_parent)
       {  $ancestors=get_post_ancestors($post->ID);
          $root=count($ancestors)-1;
          $parent = $ancestors[$root];
          $top_page = get_post($parent); }
       else
       { $top_page = $post; }
       $top_page_name = $top_page->post_name;
       if ( $top_page_name != 'school' && $top_page_name != 'early-childhood-education' ) $top_page_name = 'trinity';
       ?>
       <body<?php if($top_page_name) { echo ' class="' . $top_page_name . '"'; } ?>>
       ```
   
 *  Thread Starter [pgapepper](https://wordpress.org/support/users/pgapepper/)
 * (@pgapepper)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651676)
 * I skimmed through the code a couple of times and didn’t see missing close. Either
   way, you are a rockstar and I owe you a beer. Viewing the source code after the
   fix, all pages are assigned the appropriate body classes. Again, thanks for all
   your help – I owe you big!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Dynamic body classes’ is closed to new replies.

## Tags

 * [child](https://wordpress.org/support/topic-tag/child/)
 * [css](https://wordpress.org/support/topic-tag/css/)
 * [Page styling](https://wordpress.org/support/topic-tag/page-styling/)
 * [parent](https://wordpress.org/support/topic-tag/parent/)

 * 6 replies
 * 2 participants
 * Last reply from: [pgapepper](https://wordpress.org/support/users/pgapepper/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/dynamic-body-classes/#post-1651676)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
