我看过关于如何抓取第一个图像并在帖子中显示的教程,以及关于抓取post\\u缩略图并在RSS提要中使用它的教程,但是有人知道如何抓取附在帖子上的第一个图像并在RSS提要中使用它吗?谢谢
如何抓取第一张附加到帖子中的图片并在RSS feed中显示?
3 个回复
最合适的回答,由SO网友:RodeoRamsey 整理而成
我最终使用了一个名为“RSS自定义字段图像”的插件,它对我来说是现成的。我甚至可以编辑它,将图像的大小更改为更易于管理的大小。
SO网友:Bainternet
事实上,我刚刚完成了一个需要在其提要中添加图像的网站的工作,因此我最终使用了以下内容:
function ba_post_image_feeds($content) {
global $post,$posts;
$first_img = \'\';
ob_start();
ob_end_clean();
$output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(!empty($first_img)){
$content = \'<div>\' . $first_img . \'</div>\' . $content;
}
return $content;
}
add_filter(\'the_excerpt_rss\', \'ba_post_image_feeds\');
add_filter(\'the_content_feed\', \'ba_post_image_feeds\');
SO网友:MikeNGarrett
这是另一种方法。
function add_images_to_rss($var) {
global $post;
if(has_post_thumbnail($post->ID)) {
$tid = get_post_thumbnail_id( $post->ID);
$thumb = wp_get_attachment_image_src($tid, \'thumbnail\');
$thumb_meta = wp_get_attachment_metadata($tid);
$up = wp_upload_dir();
print \'<enclosure type="\'.get_post_mime_type($tid).\'" length="\'.filesize($up[\'basedir\'].\'/\'.$thumb_meta[\'file\']).\'" url="\'.$thumb[0].\'" />\';
}
}
/* Technically this format is RSS2 only */
// add_action(\'rss_item\',\'add_images_to_rss\');
add_action(\'rss2_item\',\'add_images_to_rss\');
// add_action(\'rdf_item\',\'add_images_to_rss\');
// add_action(\'atom_entry\',\'add_images_to_rss\');
结束