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);
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?
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/
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 . '"'; ?>>
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.
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 . '"'; } ?>>
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!