Using body_class()
, by default, WordPress adds classes like parent-pageid-43
to it. This only goes one level deep, though.
The code below works for any ancestors of your parent pages, not just children. It relies on the is_page_or_ancestor plugin for that.
As kaiser said, body classes are the way to go. You can define the name of the classes for each template yourself by modifying the $parents
array.
add_filter('body_class', 'my_body_class', 10, 2);
function my_body_class($wp_classes, $extra_classes)
{
// List of parent pages with custom template
$parents = array(
// page ID => body class
43 => 'template-a',
57 => 'template-b',
);
// Loop over each parent
foreach ($parents as $page_id => $class)
{
// http://wordpress.org/extend/plugins/is-page-or-ancestor/
if (is_page_or_ancestor($page_id))
{
// Add body class
$wp_classes[] = $class;
}
}
// Add the extra classes back untouched
return array_merge($wp_classes, (array) $extra_classes);
}