Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
?>
@sheetal-mandyal

This comment has been minimized.

Show comment
Hide comment
@sheetal-mandyal

sheetal-mandyal Jul 19, 2017

it didn't worked for me :(

sheetal-mandyal commented Jul 19, 2017

it didn't worked for me :(

@idarek

This comment has been minimized.

Show comment
Hide comment
@idarek

idarek Aug 5, 2017

Is it still valid? are this will work straight away or need to wait 24h for rss readers to refresh?

idarek commented Aug 5, 2017

Is it still valid? are this will work straight away or need to wait 24h for rss readers to refresh?

@ibda347

This comment has been minimized.

Show comment
Hide comment
@ibda347

ibda347 Oct 25, 2017

replace
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
with this
$output = the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );

ibda347 commented Oct 25, 2017

replace
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
with this
$output = the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );

@marekale

This comment has been minimized.

Show comment
Hide comment
@marekale

marekale 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;
}
?>

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