我经常使用这个函数来提取帖子的图像:
function first_post_image($content) {
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
然后我用以下代码显示图像
<?php $postimage = first_post_image(get_the_content()); ?> // catch the image
<img src="<?php echo $postimage; ?>" /> //view the image
现在我尝试在我的上一个模板(wp)中使用此功能,但图像没有出现。我可以看到标签 img 和 src 属性,但它是空的。
有人可以帮助我吗?谢谢