1

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' );
1
  • this sounds wrong. what would be a reason for doing such a thing? Commented Apr 27, 2018 at 12:10

2 Answers 2

2

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);
0
1
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.

1
  • Hmm... I'm pretty sure that hentry class is already removed with code posted in question... Commented Apr 27, 2018 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.