• 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(); ?>

    `

    • This topic was modified 2 years, 4 months ago by onlynoone.
    • This topic was modified 2 years, 4 months ago by onlynoone. Reason: tried to fix 'code' view
    • This topic was modified 2 years, 4 months ago by onlynoone.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter onlynoone

    (@onlynoone)

    Update: I found the culprit/problem.

    The code works perfectly when I use the gallery=>direct upload option. But starts to fail when I want to go gallery=>Media Library to upload images. So the problem lies with the Media Library, it doesn’t get recognised for some reason.

    I haven’t figured out how to fix it yet, so any suggestions welcome.

    Hi onlynoone,

    Using WP_Query with post_parent set should work if the images were attached to the post via the media gallery (not just in the raw HTML of the post content via an img tag). Additionally, there are utility functions like get_attached_media() that make this convenient.

    WP_Query provides much more control around how you grab posts and iterate/paginate through them, so if you need WP_Query, I would recommend removing your post_status argument in your WP_Query args and accessing the attachment ID differently while you’re in your loop:

    After you call $query_posts->the_post(); you have told WordPress to load in the attachment as the current post, so you should use the get_the_ID() utility function to get the current attachment ID rather than $query_images->posts->ID and your wp_reset_postdata() call will reset the post data back to the original post.

    $query_images->posts is accessing the raw PHP array from your WP_Query results… so you’d have to use an array index to access the ID object (i.e. $query_images->posts[0]->ID) which I don’t recommend doing.

    If you don’t need the granular control & pagination that WP_Query offers, you might want to do something a touch more simple, like so:

    <?php
    $attachments = get_attached_media('image', get_the_ID());
    if (!empty($attachments)) {
        foreach ($attachments as $image) {
            echo wp_get_attachment_image($image->ID, 'full');
        }
    } else {
        echo '<strong>Could not find any images attached to this post</strong>';
    }

    Hope this helps!

    • This reply was modified 2 years, 4 months ago by jphase.
    Thread Starter onlynoone

    (@onlynoone)

    Hello jphase,

    Thank you for your reply and helping out.

    Your code works the same as mine so far, the real problem is that I can’t target/call the images that were inserted to the post via the Media Library.

    So no direct upload to the post, it seems like this creates an issue that makes those images unattached and I guess can’t be called with wp_get_attachment_image

    So if I direct upload an image to the post, the above code(s) work but once I use the already uploaded images in the Media Library it fails.

    Moderator bcworkz

    (@bcworkz)

    If you want to use existing library images in a gallery, you cannot rely upon the attachment’s $parent property. It’s a dead end, it won’t work. You’ll need to query for attachments by some other means. If you examine how the [gallery] shortcode works, you’ll see it maintains its own list of attachment IDs of those images belonging in that gallery. Such IDs could be saved in portfolio meta data. Other possibilities would be assigning specific taxonomy terms or meta values to the attachment that would distinguish images in a specific portfolio’s gallery from all other images.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show all attached images of a specific post’ is closed to new replies.