Forums

Filter out categories and tags from post_class (3 posts)

  1. Elpie
    Member
    Posted 3 years ago #

    The categories and tags are in the array that builds the post_class in post-template.php.

    I want to use post_class and love having the ability to add classes, but am stuck trying to remove the tags and categories.

    Anyone know how to remove those two without hacking the core?

  2. SimonBoba
    Member
    Posted 2 years ago #

    Hi.

    Write this instead of post_class in your template:

    <?php
    $post_classes = get_post_class();
    foreach ($post_classes as $key => $value) {
    	$pos = strripos($value, 'tag-');
    	if ($pos !== false) {
    		unset($post_classes[$i]);
    	}
    }
    $post_classes= implode(' ', $post_classes);
    echo 'class="'.$post_classes.'"';
    ?>

    Not the most elegant solution, but it does the job. If you want to remove categories too, just do the same thing with 'category-' in the loop.

  3. Elpie
    Member
    Posted 2 years ago #

    Cheers - I'm actually filtering out all dynamic classes now as they accounted for an increase of 50% in file sizes on my themes.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags