Remove classes from post_class()
-
20-03-2021 - |
Question
I am trying to tidy up the post_class
function.
I've managed to remove "hentry" class using the filter below, but I would also like to remove the "post-id", "type-" and "status-" classes as well. How can I use my filter to do this?
function lsmwp_remove_postclasses( $classes ) {
$classes = array_diff( $classes, array( 'hentry' ) );
return $classes;
}
add_filter( 'post_class','lsmwp_remove_postclasses' );
Solution
One way would be to use preg_match
and remove classes that matches given patterns, but... Since we know $post_id
, we can use your code with a tiny modification:
function lsmwp_remove_postclasses($classes, $class, $post_id) {
$classes = array_diff( $classes, array(
'hentry',
'post-' . $post_id,
'type-' . get_post_type($post_id),
'status-' . get_post_status($post_id),
) );
return $classes;
}
add_filter('post_class', 'lsmwp_remove_postclasses', 10, 3);
OTHER TIPS
add_filter( 'post_class', 'remove_hentry_function', 20 );
function remove_hentry_function( $classes ) {
if( ( $key = array_search( 'hentry', $classes ) ) !== false )
unset( $classes[$key] );
return $classes;
}
Here is reference
It removing the "hentry" class.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange