Skip to content

Instantly share code, notes, and snippets.

@pradeepdotco
Created October 3, 2015 10:54
Show Gist options
  • Save pradeepdotco/761c6bc617b53ea6fc38 to your computer and use it in GitHub Desktop.
Save pradeepdotco/761c6bc617b53ea6fc38 to your computer and use it in GitHub Desktop.
Function to add featured image to RSS feed
<?php
add_filter( 'the_content', 'featured_image_in_feed' );
function featured_image_in_feed( $content ) {
global $post;
if( is_feed() ) {
if ( has_post_thumbnail( $post->ID ) ){
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
$content = $output . $content;
}
}
return $content;
}
?>
@marekale
Copy link

marekale commented Nov 9, 2017

I would rather use 'the_content_feed' filter, and 'the_excerpt_rss' filter to make it work with excerpts as well. Then there's no need for is_feed() check, because filters are specific for feed. And we need to check if the feed is for comments or for posts. We don't need post's featured image in comments feed.

<?php
add_filter( 'the_content_feed', 'featured_image_in_feed' );
add_filter( 'the_excerpt_rss', 'featured_image_in_feed' );
function featured_image_in_feed( $content ) {
    global $post, $wp_query;
    if( ! $wp_query->is_comment_feed) { 
        if ( has_post_thumbnail( $post->ID ) ){
            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
            $content = $output . $content;
        }
    }
    return $content;
}
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment