Site logo

Archived topic

RSS Feed Image Not Displayed After Adding Function

7 replies · Started by Rachel on June 6, 2022

Viewing posts 1–8 of 8

Hi there,

I have added the following snippet to my site (listed in private info) to enable the featured image for a post to be displayed in the RSS Feed:

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

However, when I check the feed, there are no image files listed in the feed for each post.

Can you please advise if there is an additional step required for the images to be fed through to the RSS feed? Or will it only display for future posts published?

Thanks,
Rachel

Hi,

I checked the quotation marks aren't slanted.

The snippet I added was provided by Tom at Generatepress in the support forum to another user a while back - so is this code no longer correct? I have my site set up to show the excerpt not the full article, so should this piece of code work in that instance?

Thanks,
Rachel

Hi Rachel,

That code should work.

Example: https://share.getcloudapp.com/7Ku6yJxo

If it doesn’t I tested this code as well and it seems to be working as an alternative:

function wpcontent_featured_image_in_feed( $content ) {
    global $post;
    if( has_post_thumbnail( $post->ID ) ) {
        $content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content;
    }
    return $content;
}
add_filter( 'the_excerpt_rss', 'wpcontent_featured_image_in_feed' );
add_filter( 'the_content_feed', 'wpcontent_featured_image_in_feed' );

Go to Settings > Reading, then set a different value in "Syndication feeds show the most recent" to refresh the feed.

Kindly let us know how it goes.

I have the featured image disabled for posts as I am using a header element with a background image - could this be why the images aren't displayed for the posts in the RSS feed? Each post has a featured image, I just don't have it selected in the customiser to display on each post.

That is interesting - if I look at the RSS code for the feed, there is no image URL listed for each item - so not sure how that is being pulled!

I have an RSS email going out early next week so I will check to see if it was able to pull the images. I am assuming it will if feedly can see them.

Thanks for your help.

I see. Hope the email goes well! You’re welcome Rachel!

This archived topic is closed to new replies.