Display attached images of a page or post that are insetred using gallery


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();
, , sialfa 4 years 2019-11-05T05:43:53-05:00 0 Answers 76 views 0

Leave an answer