Home > Back-end >  How to remove category class from post page?
How to remove category class from post page?

Time:06-07

I want to remove category class from blog post page. Suppose I have 2 category one is fashion and another one is business. in blog post wordpress add class like "category-fashion category-business" . I want to remove all the classes for each category. I tried using jquery but class name hide on inspect element but not in view source from browser. if possible I want to remove using function.php file. below is my jquery code.

<script>
jQuery(window).on("load", function() {

    <?php
        $categories = get_categories();
        foreach($categories as $category) {
            echo 'jQuery(".category-' . strtolower($category->name) .'").removeClass("category-'. strtolower($category->name) . '");';
        } 
    ?>
});
</script>

CodePudding user response:

please try this code in your theme's functions.php file, hope it will work in the blog page's post class.

add_filter( 'post_class','remove_category_post_classes' );
function remove_category_post_classes( $classes ) {
        $categories = get_categories();
         
        foreach ( $categories as $category ) {
            $remove_classes[] = 'category-'.$category->slug;
        }
        
        $classes = array_diff($classes, $remove_classes);
    
    return $classes;
}

If you want to remove the classes from the body class then you can just replace the filter with this code

add_filter( 'body_class','remove_category_post_classes' );
  • Related