Customizing the WordPress Body Class: http://darrinb.com/customizing-the-wordpress-body_class-function/
<?php | |
/** | |
* Add dynamic classes to the WP body_class() function | |
* | |
* updated: 01/06/16 | |
*/ | |
function _dbdb_body_classes( $classes ) { | |
global $wp_query, $post; | |
// if it's the front page of the site | |
if ( is_front_page() ) { | |
$classes[] = 'front-page'; | |
} | |
// url paths | |
if ( is_singular() ) { | |
// Get the queried post | |
$post_id = get_queried_object_id(); | |
$post_type = get_post_type( $post_id ); | |
$post_type_object = get_post_type_object( $post_type ); | |
if ( 'post' !== $post_type ) { | |
if ( false !== $post_type_object->rewrite ){ | |
if ( !empty( $post_type_object->rewrite['slug'] ) ){ | |
$path = $post_type_object->rewrite['slug']; | |
$path = trim( $path, '/' ); | |
if ( !empty( $path ) ) { | |
$matches = explode('/', $path); | |
if ( is_array( $matches ) ) { | |
$matches = array_reverse( $matches ); | |
$parent_title = sanitize_title( $matches[0] ); | |
$classes[] = 'parent-pagename-'.$parent_title; | |
if ( count($matches) > 1 ) { | |
$is_first = true; | |
foreach ( $matches as $ancestor ){ | |
if ( $is_first ) { | |
$is_first = false; | |
continue; | |
} | |
$ancestor_title = sanitize_title( $ancestor ); | |
$classes[] = 'ancestor-pagename-'.$ancestor_title; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
// the $post object | |
$post_obj = ( property_exists( $wp_query, 'post' ) && isset( $wp_query->post ) ) ? true : false; | |
$post_obj_parent = ( $post_obj && property_exists( $wp_query->post, 'post_parent' ) ) ? true : false; | |
// if there is no parent ID and it's not a single post page, category page, or 404 page, give it a class of "parent-page" | |
if ( $post_obj_parent && $wp_query->post->post_parent < 1 && !is_single() && !is_archive() && !is_404() ) { | |
$classes[] = 'parent-page'; | |
}; | |
// if the page/post has a parent, it's a child, give it a class of its parent name | |
if ( $post_obj_parent && $wp_query->post->post_parent > 0 ) { | |
$parent_title = get_the_title($wp_query->post->post_parent); | |
$parent_title = sanitize_title($parent_title); | |
$classes[] = 'parent-pagename-'.$parent_title; | |
}; | |
// if the page/post has ancestors, get all ancestors' names | |
$ancestors = array(); | |
if ( isset($wp_query->queried_object) && isset($wp_query->queried_object->ancestors) ) { | |
$ancestors = $wp_query->queried_object->ancestors; | |
} | |
if ( is_array($ancestors) && !empty($ancestors) ) { | |
foreach($ancestors as $ancestor) { | |
$ancestor_title = get_the_title($ancestor); | |
$ancestor_title = sanitize_title($ancestor_title); | |
$classes[] = 'ancestor-pagename-'.$ancestor_title; | |
} | |
} else { | |
$classes[] = 'top-level-ancestor'; | |
} | |
// get the post categories | |
$categories = ( isset($wp_query->queried_object) ) ? get_the_category($wp_query->queried_object_id) : false; | |
if ( $categories ) { | |
foreach ( $categories as $category ) { | |
$classes[] = 'in-category-'.$category->category_nicename; | |
} | |
} | |
// add a class equal to the name of post or page | |
if ( isset($wp_query->queried_object) && isset($wp_query->queried_object->post_name) ) { | |
$classes[] = 'pagename-'.$wp_query->queried_object->post_name; | |
} | |
// author | |
if ( function_exists( 'is_multi_author' ) && ! is_multi_author() ) { | |
$classes[] = 'single-author'; | |
} | |
// single pages | |
if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) && ! is_page_template( 'sidebar-page.php' ) ) { | |
$classes[] = 'singular'; | |
} | |
$classes = array_filter($classes); | |
return array_unique($classes); | |
}; | |
add_filter('body_class','_dbdb_body_classes'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment