[WordPress教程] wordpress获取文章内所有图片 -静鱼客栈

2021-09-24 0 1,611

对于制作WordPress图片主题的小伙伴这个wordpress获取文章内所有图片问题我想是大家一直在考虑的问题,当然或者不是制作图片主题但是列表上需要也会用到下面用一个简单额列子来表示wordpress获取文章内所有图片。


<?php
$SoImages  = '~<img [^\>]*\ />~';
preg_match_all($SoImages,$post->post_content,$Images);
$PictureAmount=count($Images[0]);
for($i=0;$i<$PictureAmount;$i++){
echo $Images[0][$i];
}
?>

你也可以把以上代码封装成可调用的函数,下面可能大家还会用到无img标签的wordpress获取文章内所有图片调用方法


<?php 
wp_enqueue_script('view-image.min');//加载灯箱脚本 
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $post->post_content, $thePics );
$allPics = count($thePics[1]);
if($allPics > 0 ){
    foreach($thePics[1] as $key => $value){?>
		<div class="gallery-item column col-3">
			<a class="image-link chocolat-image wow fadeIn" title="<?php the_title(); ?>" href="<?php echo $value;?>" rel="external nofollow" >
				<div class="image">
					<img src="<?php echo $value;?>" alt="<?php the_title(); ?>"/>
					<div class="gallery-overlay">
						<div class="gallery-info">
							<h4><?php the_title(); ?></h4>
						</div>
					</div>
				</div>
			</a>
		</div>
<?php }}?>

我自己写了一小段来针对xiublog主题图像灯箱功能,希望能帮的到大家,有什么问题呢可以交流今天的wordpress获取文章内所有图片WordPress教程就到此结束。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

静鱼客栈 学习中心 [WordPress教程] wordpress获取文章内所有图片 -静鱼客栈 https://wp.muooy.cn/9126.html

静鱼客栈的帅逼站长~

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务