wordpress function to remove first image from posts with specific category

I am currently using this function to remove the first image from my posts, however, I need to remove the first image from posts with a specific category. Any help is much appreciated.

function remove_first_image ($content) {
    if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
        $content = preg_replace("/<img[^>]+>/i", "", $content, 1);
    } 
    return $content;
}
//add_filter('the_content', 'remove_first_image');

Related posts

2 comments

  1. I figured it out.

    function remove_first_image ($content) {
    
        if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
            if(has_term( array( 'entertainment', 'food', 'home', 'travel' ), 'category' ) )
                $content = preg_replace("/<img[^>]+>/i", "", $content, 1);
        } 
    
        return $content;
    
    }
    add_filter('the_content', 'remove_first_image');
    
  2. You can use in_category conditional functions

    function remove_first_image ($content) {
        if ( in_category( 'catslug' ) ) {
            $content = preg_replace("/<img[^>]+>/i", "", $content, 1);
        } 
        return $content;
    }
    //add_filter('the_content', 'remove_first_image');
    

Comments are closed.