0

I want to add the featured image to my RSS feed.

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_content();
    }
    return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail', 20, 1);
add_filter('the_content_feed', 'rss_post_thumbnail', 20, 1);

With this snippet the thumbnail is shown, but the content is not generated correct. It is shown with shortcodes and its not formatted...

When i use the snippet below, where i remove the ordering of filters, the content is shown, but the featured image is missing:

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_content();
    }
    return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
2
  • "With this snippet the thumbnail is shown, but the content is not generated correct." - you're building the output from get_the_content() not $content, and I'd guess the latter has the shortcodes expanded (via the 'the_content' filter). I can't explain the second behaviour though.
    – Rup
    Commented Feb 20, 2020 at 11:56
  • Note also that the_content_feed takes two arguments, $content and $feed_type, not 1 as you've got in your first add_filter.
    – Rup
    Commented Feb 20, 2020 at 12:01

1 Answer 1

0

I just found the right solution:

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post->ID)) {
        $output = '<p>' . get_the_post_thumbnail($post->ID) . '</p>';
    }
    return $output . $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail', 20, 1);
add_filter('the_content_feed', 'rss_post_thumbnail', 20, 1);
1
  • If there's no thumbnail then you're using $output without setting in though, which will trigger a 'Notice' in debug mode.
    – Rup
    Commented Feb 20, 2020 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.