Loading
0

WordPress 获取上传到当前文章的所有图片

这里所说的不是遍历文章内容里的图片地址来获取所有图片,而且获取上传到当前文章的媒体库的所有图片,因为有时候我们回去单独做一个相册,而文章内容里的图片可能有其他不相干的图片存在,所有获取上传到当前文章的媒体库图片是最合适的。

下面的代码是通过获取所有图片来弄一个类似淘宝产品图切换的效果,swiper版本是4.5。

function MBThemes_thumbnail_gallery(){
global $post;
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => 6,
'post_status' => 'any',
'post_parent' => $post->ID
));
if ($attachments){
$count = count($attachments);
if($count){
?>
<link rel="stylesheet" href="<?php%20bloginfo('template_url');?>/static/css/swiper.min.css">
<div class="swiper-container gallery-top" id="gallery-top<?php echo $post->ID;?>">
<div class="swiper-wrapper">
<?php 
foreach ($attachments as $attachment){
echo '<div class="swiper-slide"><img src="'.wp_get_attachment_url($attachment->ID).'"></div>';
}
?>
</div>
</div>
<div class="swiper-container gallery-thumbs" id="gallery-thumbs<?php echo $post->ID;?>">
<div class="swiper-wrapper">
<?php 
foreach ($attachments as $attachment){
echo '<div class="swiper-slide"><img src="'.wp_get_attachment_url($attachment->ID).'"></div>';
}
?>
</div>
</div>
<script src="<?php%20bloginfo('template_url');?>/static/js/swiper.min.js"></script>
<script>
var galleryThumbs = new Swiper('#gallery-thumbs<?php echo $post->ID;?>', {
spaceBetween: 10,
slidesPerView: <?php echo ($count>6)?$count:6;?>,
freeMode: true,
autoHeight: true,
watchSlidesVisibility: true,
watchSlidesProgress: true,
});
var galleryTop = new Swiper('#gallery-top<?php echo $post->ID;?>', {
spaceBetween: 10,
thumbs: {
swiper: galleryThumbs
}
});
</script>
<?php 
}
}
}

特别声明:1、本站仅提供源码学习下载,使用者需具备一定的技术基础,源码费用仅为站长辛苦整理费,不代表源码自身价值也不包含任何服务,如需完美运营请到官方购买。如需搭建、二开、bug修复等服务需额外收费,如果源码侵犯了您的利益请联系客服处理! 2、用户必须遵守《计算机软件保护条例(2013修订)》第十七条:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。鉴于此条例,用户从本平台下载的全部源码(软件)仅限学习研究,未经版权归属者授权不得商用,若因商用引起的版权纠纷,一切责任均由使用者自行承担,本平台所属公司及其雇员不承担任何法律责任。本站邮箱:weituiw@qq.com