从RSS提要上的帖子中获取精选图片

时间:2020-02-20 作者:LovinQuaQua

我想将特色图片添加到我的RSS提要中。

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);
此代码段显示缩略图,但生成的内容不正确。它显示为短代码,且未格式化。。。

当我使用下面的代码段时,我删除了过滤器的顺序,显示了内容,但缺少了特征图像:

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

1 个回复
SO网友:LovinQuaQua

我刚刚找到了正确的解决方案:

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