0
在我的主页,我试图输出每个帖子的第一个图像抽丝第一形象,我能成功地做到这一点在我functions.php中使用此代码:WordPress的:从画廊
function catch_that_image() {
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)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
,然后我把它在我的循环是这样的:
<img src=”<?php catch_that_image(); ?>” />
这种方法的问题是,如果我把在同一职位的画廊将无法正常工作。我很困惑,因为该帖子的输出仍然呈现img标记,我的假设是,catch_that_image()应该拦截该标记?我的想法不正确吗?有没有更好的方法来处理这个问题?
这似乎已经奏效!谢谢! –
它呈现图像的文本路径但不是图像本身的任何解决方案? –
我的错误我试图让它没有图像标签:P反正谢谢,但现在我想添加我的自定义大小。任何想法如何做? –