I've finally added a custom metabox to link some images to some custom urls, (thanks to the Sally CJ for the help in troubleshooting my previous issue with adding a metabox), I need now to adapt this plugin code that will add a swiper slider using a custom shortcode. I've noticed that It will load all the media library images and I don't want this, I want to display only the gallery images that are attached to the post or page where the shortcode is used.
I've tried in past with get_post_gallery_images()
but it will not work, so I've used the WP_Query
that is causing this problem. Any help will be appreciated.
<?php
public function imageSlider( array $attr ) : string
{
ob_start();
extract(shortcode_atts(
array(
'class' => null,
'id' => null
), $attr)
);
$this->args = [
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
];
$this->images = new WP_Query( $this->args );
$this->img = [];
$this->link = [];
foreach( $this->images->posts as $image ){
$this->link[] = get_post_meta($image->ID, 'image_link_url', true);
$this->img[] = wp_get_attachment_url( $image->ID );
}
?>
<div class="swiper-container <?php echo $class; ?>" id="<?php echo $id; ?>">
<div class="swiper-wrapper">
<?php
foreach( $this->img as $i => $src ){
?>
<a href="<?php echo $this->link[$i]; ?>">
<img class="swiper-slide img-fluid w-100" src="<?php echo $src; ?>">
</a>
<?php
}
?>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
<?php
return ob_get_clean();
}
?>