对于制作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教程就到此结束。