Skip to content

Instantly share code, notes, and snippets.

@mohsinrasool
Created August 19, 2015 11:58
Show Gist options
  • Save mohsinrasool/eb28eb51a933b7e51c83 to your computer and use it in GitHub Desktop.
Save mohsinrasool/eb28eb51a933b7e51c83 to your computer and use it in GitHub Desktop.
Add Page slug to the body_class function
// Add to the body_class function
function condensed_body_class($classes) {
global $post;
// add a class for the name of the page - later might want to remove the auto generated pageid class which isn't very useful
if( is_page()) {
$pn = $post->post_name;
$classes[] = "page-".$pn;
}
// add a class for the parent page name
if ( is_page() && $post->post_parent ) {
$post_parent = get_post($post->post_parent);
$parentSlug = $post_parent->post_name;
$classes[] = "parent-".$parentSlug;
}
// add class for the name of the custom template used (if any)
$temp = get_page_template();
if ( $temp != null ) {
$path = pathinfo($temp);
$tmp = $path['filename'] . "." . $path['extension'];
$tn= str_replace(".php", "", $tmp);
$classes[] = "template-".$tn;
}
return $classes;
}
add_filter('body_class', 'condensed_body_class');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment