Show all attached images of a specific post
-
Hello everyone,
I’ve been delving into wordpress recently to create a basic custom template.
It’s a portfolio template and as such I’m creating a custom post template with built-in carousel (no-plugin).I’ve been scrapping some code together and it works only partially.
The post page is built with Swiper.js and wp_query but it sadly shows all pictures in the media gallery and not just the ones of the specific post.When using
'post_parent' => $post->ID,
in the array to load only images from the specific post it breaks the loop and doesn’t show any pictures at all. So when'post_parent' => $post->ID,
removed it shows all of the images of the whole media gallery, if added nothing loads and I get my “No media file yet” message.I hope someone can help me to figure out how to only show the images linked to the specific/current post and not all of the images in the whole gallery “database”.
Thank you.
(code below)
<code> <div class="swiper"> <!-- Additional required wrapper --> <div class="swiper-wrapper"> <?php $query_images_args = array( 'post_type' => 'attachment', 'post_parent' => $post->ID, 'post_mime_type' => 'image,video',// video files include 'post_status' => 'inherit', 'orderby' => 'post_date', 'posts_per_page' => 30, ); $query_images = new WP_Query( $query_images_args ); if($query_images->have_posts()) : while($query_images->have_posts()) : $query_images->the_post(); ?> <!-- Slides --> <div class="swiper-slide"> <div> <?php echo wp_get_attachment_image( $query_images->posts->ID, 'full' ); ?> </div> </div> <?php endwhile; ?> <?php else : ?> <p>No media file yet/p> <?php endif; /* Restore original Post Data */ wp_reset_postdata(); ?>
`
- The topic ‘Show all attached images of a specific post’ is closed to new replies.