Skip to content

Instantly share code, notes, and snippets.

@dboutote
Last active January 11, 2016 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dboutote/834ac85e30c8e03baa3f to your computer and use it in GitHub Desktop.
Save dboutote/834ac85e30c8e03baa3f to your computer and use it in GitHub Desktop.
Customizing the WordPress post_class Filter
<?php
function my_post_classes( $classes ) {
global $multipage, $page, $post, $wp_taxonomies;
if($wp_taxonomies) {
foreach($wp_taxonomies as $taxonomy) {
$tax_array[] = $taxonomy->name;
}
$taxes = wp_get_object_terms(get_the_id(), $tax_array, array('fields' => 'all'));
if($taxes) {
foreach($taxes as $k => $v) {
$classes[] = $v->taxonomy . '_' . $v->slug;
}
}
}
// if it's a paged post
if ( $multipage ) {
$classes[] = 'paged-post';
$classes[] = 'post-page-'.$page;
}
// if it's the first page of the post
if ( 1 === $page ) {
$classes[] = 'post-first-page';
}
// return the $classes array
return $classes;
};
add_filter( 'post_class', 'my_post_classes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment