如何在RSS中包含特色图像的帖子网址?

时间:2017-03-02 15:15:18

标签: php wordpress

我在我的functions.php中有这个工作正常,我只想将特色图片链接到帖子。

add_filter( 'the_excerpt_rss', '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' => 'width:100%; margin-bottom:15px; display: inline-block;' ) );
        $content = $output . $content;
    }
}
return $content;
}

这是输出

<description><![CDATA[<img width="564" height="153" src="http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-564x153.jpg" class="attachment-medium size-medium wp-post-image" alt="checklist" style="width:100%; margin-bottom:15px; display: inline-block;" srcset="http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-564x153.jpg 564w, http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-768x208.jpg 768w, http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484.jpg 880w" sizes="(max-width: 564px) 100vw, 564px" />Teaser text is here]]></description>

我希望

<description><![CDATA[<a href="linktothepost"><img width="564" height="153" src="http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-564x153.jpg" class="attachment-medium size-medium wp-post-image" alt="checklist" style="width:100%; margin-bottom:15px; display: inline-block;" srcset="http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-564x153.jpg 564w, http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484-768x208.jpg 768w, http://mywebsite.com/blog/wp-content/uploads/2017/03/Checklist-crop-ID-85484.jpg 880w" sizes="(max-width: 564px) 100vw, 564px" /></a>Teaser text is here]]></description>

1 个答案:

答案 0 :(得分:1)

由于get_post_thumnail()只是返回一个包含标记的字符串,所以你应该能够将链接包裹起来。

if ( has_post_thumbnail( $post->ID ) ){
    $output = '<a href="' . get_permalink( $post->ID ) . '">';
    $output .= get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'width:100%; margin-bottom:15px; display: inline-block;' ) );
    $output .= '</a>';
    $content = $output . $content;
}