纯代码实现wordpress文章中图片调用
本文最后更新于 717 天前,其中的信息可能已经有所发展或是发生改变。

纯代码实现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>
//应该可以加入判断来实现,比如调用文章前两篇文章。

 

文章原文:https://www.luojiasan.com/category/182.html
文章来源:https://www.luojiasan.com
[声明]本站内容除特别声明外,均属站长手写原创内容。
1、本站所有原创文章、图片等资料,如需转载使用,必须附上来源出处及链接方式;
2、若本站侵犯到您的权益或任何版权问题,请立即告知本站,我们将及时删除并致以最深的歉意!
3、本站转载文章版权归原作者所有,内容为作者个人观点,本站只提供参考并不构成任何投资及应用建议。任何本站转载文章链接及得到的资讯、产品、服务,本站概不负责,亦不负任何法律责任。

评论

  1. a.三三
    4年前
    2020-8-04 11:45:47

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

发送评论 编辑评论


				
上一篇
下一篇