WordPress.org

Ready to get started?Download WordPress

Ideas

Exclude a class from post_class()

  1. byfiles
    Member

    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: 2 years ago #
  2. Garden Sheds
    Member

    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.

    Cheers

    Posted: 2 years ago #
  3. sdls
    Member

    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;
    }
    add_filter('post_class','remove_hentry');
    Posted: 2 years ago #
  4. poderwac
    Member

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

    Posted: 2 years ago #
  5. gr1zzly
    Member

    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.

    hth

    Posted: 1 year ago #
  6. Sheds Direct
    Member

    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: 8 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    12 Votes
  • Status

    This idea has been implemented