Last active
January 11, 2016 04:45
-
-
Save dboutote/834ac85e30c8e03baa3f to your computer and use it in GitHub Desktop.
Customizing the WordPress post_class Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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