WordPress在RSS Feed 输出自定义特色图像(缩略图)

一般来说,如果主题支持特色图像(缩略图),在主题的 functions.php 文件下加入以下代码就可以实现RSS 中输出自定义特色图像(缩略图)的功能:

//Feed 输出文章特色图像(缩略图)dreamfy.com
function dw_rss_thumbnail($content) {
	global $post; //查询全局文章
	if(has_post_thumbnail($post->ID)) { //如果有特色图像
		$output = get_the_post_thumbnail($post->ID) ; //获取缩略图
		$content = $output . $content ;
	}
	return $content;
}
add_filter('the_excerpt_rss', 'dw_rss_thumbnail');
add_filter('the_content_feed', 'dw_rss_thumbnail');

如果主题比较特殊,缩略图像是通过外链来的,则使用以下代码实现。

//Feed 输出自定义文章特色图像(缩略图)升级版 dreamfy.com
function dw_post_thumbnail($content) {
    global $post; //查询全局文章
    if ( get_post_meta($post->ID, 'thumb', true) )
    {
         $dwimage = get_post_meta($post->ID, 'thumb', true);
         $output = '<img width="580" height="150" src="'.$dwimage.'" alt="'.get_the_title().'"/> ';
         $content = $output . $content ;
    }
     return $content;
}
add_filter('the_excerpt_rss', 'dw_post_thumbnail');
add_filter('the_content_feed', 'dw_post_thumbnail');

因为每个人的主题都不同,使用上面的代码需要修改部分内容。

展开评论