Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wecodelaravel/0c48493d7f10421e34cac47d37062a28 to your computer and use it in GitHub Desktop.
Save wecodelaravel/0c48493d7f10421e34cac47d37062a28 to your computer and use it in GitHub Desktop.
Add class name for top-level parent category or page to body tag in WordPress posts, pages, and archives.
<?php
/* Add body-class for top-level parent Page or Category */
function topcatpg_body_class( $class ) {
$prefix = 'topic-'; // Editable class name prefix.
$top_cat_pg = 'home'; // Default.
global $top_cat_pg;
// Get class name from top-level Category or Page.
global $wp_query;
if ( is_single() ) {
$wp_query->post = $wp_query->posts[0];
setup_postdata( $wp_query->post );
/* Climb Posts category hierarchy, successively replacing
class name top_cat_pg with slug of higher level cat. */
foreach( (array) get_the_category() as $cat ) {
if ( !empty( $cat->slug ) )
$top_cat_pg = sanitize_html_class( $cat->slug, $cat->cat_ID );
while ( $cat->parent ) {
$cat = &get_category( (int) $cat->parent);
if ( !empty( $cat->slug ) )
$top_cat_pg = sanitize_html_class( $cat->slug, $cat->cat_ID );
}
}
} elseif ( is_archive() ) {
if ( is_category() ) {
$cat = $wp_query->get_queried_object();
$top_cat_pg = $cat->slug;
/* Climb Category hierarchy, successively replacing
class name with slug of higher level cat. */
while ( $cat->parent ) {
$cat = &get_category( (int) $cat->parent );
if ( !empty( $cat->slug ) )
$top_cat_pg = sanitize_html_class( $cat->slug, $cat->cat_ID );
}
}
} elseif ( is_page() ) {
global $post;
if ( $post->post_parent ) {
$ancestors = get_post_ancestors( $post->ID );
$root = count( $ancestors ) - 1;
$top_id = $ancestors[$root];
$top_pg = get_page( $top_id );
$top_cat_pg = $top_pg->post_name;
} else {
$top_cat_pg = $post->post_name;
}
}
$class[] = $prefix . $top_cat_pg;
return $class;
}
add_filter( 'body_class', 'topcatpg_body_class' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment