Search code examples
phpwordpressrss

Wordpress RSS feeds: Customizing the feeds for specific categories


I have two feeds, one for articles and one for news entries. I've written a function that includes the thumbnail in the feeds. In that function I choose which thumb size to show.

The problem is that for the news feed, I want one image size, and another image size for the article feed.

Is there anyway to modify the add_filter hook to only apply the function to one category? And then maybe duplicate the function, change the thumb size and the category?

Function:

function insertThumbnailRSS($content) {
    global $post;
    if ( has_post_thumbnail( $post->ID ) ){
        $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
    }
    return $content;
}

add_filter('the_excerpt_rss', 'insertThumbnailRSS');
add_filter('the_content_feed', 'insertThumbnailRSS');

Tyty!


Solution

  • Perhaps try:

    function insertThumbnailRSS($content) {
        global $post;
        if ( has_post_thumbnail( $post->ID ) && in_category(4, $post-ID) ){
            $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
        }
    
        if ( has_post_thumbnail( $post->ID ) && in_category(5, $post-ID) ){
            $content = '' . get_the_post_thumbnail( $post->ID, 'different-size' ) . '' . $content;
        }
    
        return $content;
    }
    
    add_filter('the_excerpt_rss', 'insertThumbnailRSS');
    add_filter('the_content_feed', 'insertThumbnailRSS');
    

    Take a look at the codex page for in_category().