post_classfilter-hookWP 2.7.0

Allows you to change the list of CSS classes for the current post.

This hook fires at the end of post_class().

Usage

add_filter( 'post_class', 'wp_kama_post_class_filter', 10, 3 );

/**
 * Function for `post_class` filter-hook.
 * 
 * @param string[] $classes   An array of post class names.
 * @param string[] $css_class An array of additional class names added to the post.
 * @param int      $post_id   The post ID.
 *
 * @return string[]
 */
function wp_kama_post_class_filter( $classes, $css_class, $post_id ){
	// filter...
	return $classes;
}
$classes(string[])
An array of CSS classes for the current post.
$class(string[])
An array of additional CSS classes added to the post.
$post_id(integer)
The post ID.

Examples

#1 Add a CSS class if the post has no thumbnail

When post_class() is used, the CSS class list contains has-post-thumbnail if the current post has a thumbnail, but nothing is added if it does not. Fix this with the following code:

add_filter( 'post_class', 'add_class_without_post_thumbnail', 10, 3 );

function add_class_without_post_thumbnail( $classes, $class, $post_id ){

	if( ! has_post_thumbnail( $post_id ) ){
		$classes[] = 'no-post-thumbnail';
	}

	return $classes;
}

Changelog

Since 2.7.0 Introduced.

Where the hook is called

get_post_class()
post_class
wp-includes/post-template.php 607
$classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );

Where the hook is used in WordPress

Usage not found.