post_class
This is a WordPress - post_class hook. The plugin just uses it.
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;
}Where the hook is called
woocommerce/includes/wc-template-functions.php 688
$post_classes = apply_filters( 'post_class', $post_classes, $class, $product->get_id() );
Where the hook is used in WooCommerce
woocommerce/includes/wc-template-functions.php 685
remove_filter( 'post_class', 'wc_product_post_class', 20 );
woocommerce/includes/wc-template-functions.php 691
add_filter( 'post_class', 'wc_product_post_class', 20, 3 );
woocommerce/includes/wc-template-hooks.php 14
add_filter( 'post_class', 'wc_product_post_class', 20, 3 );