Skip to content

Instantly share code, notes, and snippets.

@og-shawn-crigger
Forked from jcanfield/functions.php
Created June 6, 2013 15:12
Show Gist options
  • Save og-shawn-crigger/5722276 to your computer and use it in GitHub Desktop.
Save og-shawn-crigger/5722276 to your computer and use it in GitHub Desktop.
Add body classes to Wordpress <body> tag
// Add more options to the body class on each page (Previous the Ambrosite Plugin)
function fineideas_body_class($classes) {
// If you don't like the class prefixes I have chosen, you can change them here.
$post_name_prefix = 'postname-';
$page_name_prefix = 'pagename-';
$single_term_prefix = 'single-';
$single_parent_prefix = 'parent-';
$category_parent_prefix = 'parent-category-';
$term_parent_prefix = 'parent-term-';
$site_prefix = 'site-';
global $wp_query;
if ( is_single() ) {
$wp_query->post = $wp_query->posts[0];
setup_postdata($wp_query->post);
$classes[] = $post_name_prefix . $wp_query->post->post_name;
$taxonomies = array_filter( get_post_taxonomies($wp_query->post->ID), "is_taxonomy_hierarchical" );
foreach ( $taxonomies as $taxonomy ) {
$tax_name = ( $taxonomy != 'category') ? $taxonomy . '-' : '';
$terms = get_the_terms($wp_query->post->ID, $taxonomy);
if ( $terms ) {
foreach( $terms as $term ) {
if ( !empty($term->slug ) )
$classes[] = $single_term_prefix . $tax_name . sanitize_html_class($term->slug, $term->term_id);
while ( $term->parent ) {
$term = &get_term( (int) $term->parent, $taxonomy );
if ( !empty( $term->slug ) )
$classes[] = $single_parent_prefix . $tax_name . sanitize_html_class($term->slug, $term->term_id);
}
}
}
}
} elseif ( is_archive() ) {
if ( is_category() ) {
$cat = $wp_query->get_queried_object();
while ( $cat->parent ) {
$cat = &get_category( (int) $cat->parent);
if ( !empty( $cat->slug ) )
$classes[] = $category_parent_prefix . sanitize_html_class($cat->slug, $cat->cat_ID);
}
} elseif ( is_tax() ) {
$term = $wp_query->get_queried_object();
while ( $term->parent ) {
$term = &get_term( (int) $term->parent, $term->taxonomy );
if ( !empty( $term->slug ) )
$classes[] = $term_parent_prefix . sanitize_html_class($term->slug, $term->term_id);
}
}
} elseif ( is_page() ) {
$wp_query->post = $wp_query->posts[0];
setup_postdata($wp_query->post);
$classes[] = $page_name_prefix . $wp_query->post->post_name;
}
if ( is_multisite() ) {
global $blog_id;
$classes[] = $site_prefix . $blog_id;
}
return $classes;
}
add_filter('body_class', 'fineideas_body_class');
/**
* WordPress' missing is_blog_page() function. Determines if the currently viewed page is
* one of the blog pages, including the blog home page, archive, category/tag, author, or single
* post pages.
*
* @return bool
*/
function is_blog_page() {
global $post;
//Post type must be 'post'.
$post_type = get_post_type($post);
//Check all blog-related conditional tags, as well as the current post type,
//to determine if we're viewing a blog page.
return (
( is_home() || is_archive() || is_single() )
&& ($post_type == 'post')
) ? true : false ;
}
// Category ID in Body and Post class
function category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes [] = ‘cat-' . $category->cat_ID . ‘-id';
return $classes;
}
//add_filter('post_class', 'category_id_class');
//add_filter('body_class', 'category_id_class');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment