admin管理员组

文章数量:1122846

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');

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');
Share Improve this question asked Feb 20, 2020 at 11:42 LovinQuaQuaLovinQuaQua 733 silver badges18 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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);

本文标签: Get featured image from post on RSS feed