Exclude a class from post_class()

  1. byfiles

    The post_class() function in WordPress is pretty darn useful. I was in a circumstance where I wanted to add an additional class to what that was outputting and it worked like a charm, but I didn't know how to remove classes. Glad to have the matter solved now.

    Posted: 7 years ago #
  2. Garden Sheds

    I had a look around for the Post_class filter, but couldn't see it. Can you let me know where i'm going wrong please. Granted, I am using the internet connection to my garden shed at the bottom of the garden so it's possible it's just not loading properly.


    Posted: 7 years ago #
  3. sdls

    Thanks Adam,
    that was real helpful.
    in case anybody else saw that and was wondering how to implement

    function remove_hentry( $classes ) {
    	$classes = array_diff($classes, array('hentry'));
    	return $classes;
    Posted: 7 years ago #
  4. poderwac

    I am very happy that I have found this thread, thanks a lot Adam. You have helped more than you know.

    Posted: 6 years ago #
  5. gr1zzly

    Yup, good idea!

    @Mark / t31os -- how can you not see why class pollution is a bad thing. 'classitis' is a bad thing. Being stuck with a load of classes you don't want or need just because someone wrote a plugin or function that adds them is not a fun situation to be in. By using more specific selectors to 'work around' it is a flawed solution at best, and can lead to over-rides of over-rides of over.... well you get the point (I hope).

    In the mean-time PHP to the rescue:

    while( have_posts() ) : the_post();
    	$classes = get_post_class();
    	if( in_array('unwanted-class', $classes) ) unset( $classes('unwanted-class') ); ?>
    	<div id="<?php the_ID(); ?>" class="<?php implode(' ', $classes); ?>">

    It would be easy to make this into a function and keep it in your 'functions.php' file. You could also omit the [IF] check and just use the 'unset()' command for brevity.


    Posted: 6 years ago #
  6. Sheds Direct

    I to am looking for the Post_class filter,and like another poster I am not in a garden shed and have perfect fibre connection so there is nothing to hinder the page loading.

    Posted: 5 years ago #
  7. kenanigans

    THANKS YOU GUYS! I know this was like from 2 years ago, but still awesome. Thanks.

    Posted: 4 years ago #
  8. m@rk

    Yep, reasons why you might want to exclude specific class(es) with an explaining example may get found at swampsidestudio.com/remove-wordpress-hentry-class.

    Posted: 3 years ago #
  9. Hi Adam, thanks for the assistance in solving this issue, we are not in a garden shed or gardenm workshop and we appreciate your assistance

    Posted: 2 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    14 Votes
  • Status

    This idea has been implemented