Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.