如何将"featured image"添加到谷歌警报 xml



我正在使用Google Alerts RSS提要来填充我网站上的侧边栏小部件。它目前显示文章的链接和发布日期,但我还想在每个项目旁边显示缩略图。阅读该问题后,我发现问题是图像不存在在Google AlertsXML中。是否有解决方法或代码可以从每个源项目(文章(中获取特色图像或缩略图并将其显示在我的侧边栏中?

我已经尝试了与将图像添加到RSS提要相关的所有插件,结果为负面。我尝试了几个代码片段,结果是负面的。

这是一个无法正常工作的代码片段:

`add_action( 'rss2_item', 'add_post_featured_image_as_rss_item_enclosure' 
);
function add_post_featured_image_as_rss_item_enclosure() {
    if ( ! has_post_thumbnail() )
        return;
    $thumbnail_size = apply_filters( 'rss_enclosure_image_size', 
'thumbnail' );
    $thumbnail_id = get_post_thumbnail_id( get_the_ID() );
    $thumbnail = image_get_intermediate_size( $thumbnail_id, 
$thumbnail_size );
    if ( empty( $thumbnail ) )
        return;
    $upload_dir = wp_upload_dir();
    printf( 
        '<enclosure url="%s" length="%s" type="%s" />',
        $thumbnail['url'], 
        filesize( path_join( $upload_dir['basedir'], $thumbnail['path'] ) 
), 
        get_post_mime_type( $thumbnail_id ) 
    );
}

所有尝试的插件/代码片段都没有产生任何错误消息,它们什么也没做。

它目前是什么样子的我希望它是什么样子

尝试将以下代码复制到您的函数.php或自定义插件中。这将在您的 rss2 提要(应 yourdomain.com/feed/(中创建一个有效的外壳标记,其中包含指向全尺寸特色图像的链接。

add_action( 'rss2_item', 'rss_feed_add_featured_image_enclosure' );
function rss_feed_add_featured_image_enclosure()
{
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( get_the_ID() ) ) {
    $attachment_id = get_post_thumbnail_id(get_the_ID());
    $url = get_the_post_thumbnail_url(get_the_ID(),'full');
    $length = filesize(wp_get_original_image_path($attachment_id));
    $type = get_post_mime_type($attachment_id);
    echo '<enclosure url="' . $url . '" length="' . $length . '" type="' . $type . '" />
';}}

最新更新