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.

public function imageSlider( array $attr ) : string


        '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">
            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; ?>">
          <div class="swiper-pagination"></div>
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>

    return ob_get_clean();
  • Actually, why not just create your own custom shortcode using add_shortcode()? Or use the standard [gallery] shortcode? Or the Gallery block in the block editor. But looking at the code, you can try adding 'post_parent' => get_the_ID() to the WP query args ($this->args). That would give you the attachments attached to the current post/page.
    – Sally CJ
    Nov 5, 2019 at 12:55
  • It's part of a custom plugin for shortcodes, I've posted only the code that I need to fix. I will try your suggestion.
    – sialfa
    Nov 5, 2019 at 13:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.