本文最后更新于 1313 天前,其中的信息可能已经有所发展或是发生改变。
纯代码实现wordpress文章中的图片调用,毕竟插件多了影响网站速度,也容易引起冲突。
function catch_that_image($picnum=0) {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = '';
if(empty($matches[1])) $first_img = "/wp-content/themes/srx/images/default.jpg";//默认图片地址
else $first_img = $matches [1] [0];
if($picnum>0) return $matches[1];
else return $first_img;//返回图片链接
}
调用文章的第一张图片:
<?php echo catch_that_image();?>
遍历文章中所有图片
//$all_pic=catch_that_image(2);用参数2调用函数,返回值存入数组
<div class="container">
<?php /*本过程处理本文章图片集
$all_pic=catch_that_image(2);//用参数2调用函数,返回值存入数组
if (count($all_pic)>0) { //文章图片数大于1才显示图片集
foreach ($all_pic as $pics=>$pic) {
echo '<ul><div class="layui-tab-item layui-show"><img src="'.$pic.'"></div></ul>';
}
}*/
?>
</div>
//应该可以加入判断来实现,比如调用文章前两篇文章。








html代码被解析了导致出现空白