Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created April 13, 2012 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterwilsoncc/2374514 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/2374514 to your computer and use it in GitHub Desktop.
Soo many WordPress body & post classes.
<?php
function filter_body_class($classes, $custom_classes ){
//avoid putting in too many
unset($classes);
$classes = array();
if (is_home() OR is_archive()) {
$classes[] = 'list';
if ( 'post' == get_post_type() ) {
$classes[] = 'all_blog';
}
}
if (is_search()) {
$classes[] = 'list';
}
if (is_singular() or is_404()) {
$classes[] = 'singular';
// $post_slug = $wp_query->post->post_name;
// $classes[] = 'slug-' . sanitize_html_class($post_slug);
if ( ( 'post' == get_post_type() ) OR ( 'attachment' == get_post_type() ) ) {
$classes[] = 'all_blog';
}
}
if ( is_page_template() ) {
$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' );
}
if ( ! empty( $custom_classes ) ) {
if ( !is_array( $custom_classes ) )
$custom_classes = preg_split( '#\s+#', $custom_classes );
$classes = array_merge( $classes, $custom_classes );
} else {
// Ensure that we always coerce class to being an array.
$custom_classes = array();
}
return array_unique( $classes );
}
add_filter( 'body_class', 'filter_body_class', 10, 2 );
function filter_post_class($classes, $custom_classes = null, $post_id = null) {
//avoid putting in too many
unset($classes);
$classes = array();
$classes[] = 'hentry';
$classes[] = 'post-' . $post_id;
$post_alt = &$this->post_alt;
$post_alt++;
$classes[] = 'p' . $post_alt;
$post_alt % 2 ? null : $classes[] = 'alt';
// $classes[] = 'author-' . sanitize_title_with_dashes(strtolower(get_the_author()));
// $classes[] = get_the_tags() ? null : 'untagged';
if ( ! empty( $custom_classes ) ) {
if ( !is_array( $custom_classes ) )
$custom_classes = preg_split( '#\s+#', $custom_classes );
$classes = array_merge( $classes, $custom_classes );
} else {
// Ensure that we always coerce class to being an array.
$custom_classes = array();
}
return array_unique( $classes );
}
add_filter( 'post_class', 'filter_post_class', 10, 3 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment