Skip to content

Instantly share code, notes, and snippets.

@mattradford
Last active January 23, 2018 12:50
Show Gist options
  • Save mattradford/53b78a87c87792495d82 to your computer and use it in GitHub Desktop.
Save mattradford/53b78a87c87792495d82 to your computer and use it in GitHub Desktop.
Body class on page and descendants
// Add class of "hub-$template_colour" to a page and its descendants
// Adapted from http://techtabby.com/how-to-add-body-class-to-page-and-descendants-wordpress/
function tend_parent_body_class( $classes ) {
if( is_page() ) {
$parents = get_post_ancestors( get_the_ID() );
$id = ($parents) ? $parents[count($parents)-1]: get_the_ID();
if ($id) {
if(get_field('colour_picker', $id )) {
$template_colour = get_field('colour_picker', $id);
$classes[] = 'hub-' . $template_colour;
}
} else {
$ancestor_id = get_the_ID();
if(get_field('colour_picker', $ancestor_id )) {
$template_colour = get_field('colour_picker', $ancestor_id);
$classes[] = 'hub-' . $template_colour;
}
}
}
return $classes;
}
add_filter( 'body_class', 'tend_parent_body_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment