0

I have a setup, where I want to achieve that I have a big Picture of a product (Post-image), and then below a list of other thumbnails, which can be opened in a Lightbox. I made this, by querying the post-image in the appropriate large size, and then querying the attachment images through a WP_Query post-type. But, the problem is, that the Query get also the post-thumbnail, and not only the images attached to the post. This is the code

<div class="product-img">

    <?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
    <?php // get fullimage
    $thumb_id = get_post_thumbnail_id( $post->ID ); 
    $image = wp_get_attachment_image_src( $thumb_id,'full' ); ?>

        <a href="<?php echo $image[0]; ?>" title="<?php the_title(); ?>" rel="lightbox">
            <?php the_post_thumbnail( 'large' ); // large image for the single post ?>
        </a>
    <?php endif; ?>

    <div class="other-images">
    <?php /* QUery attached images */
    $images_query_args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'post_mime_type' => 'image',
        'post_parent' => $post->ID
    );

    $images_query = new WP_Query( $images_query_args );

    if ( $images_query->have_posts() ) : while ( $images_query->have_posts() ) : $images_query->the_post();
    ?>

    <a href="<?php echo wp_get_attachment_image_url( get_the_ID(), 'large' ); ?>" title="<?php the_title(); ?>" rel="lightbox">
        <?php   // Output the attachment image
        echo wp_get_attachment_image( get_the_ID(), 'thumbnail' ); ?>
    </a>

    <?php
    endwhile; endif;
    wp_reset_postdata();
    ?>

</div>

How can I make the image_query not to display the post-image, but only the others? Or, if there exists a plugin which handles the attached galleries better (eg. custom sorting-order, better visualizing which image is attached) than the native WP, I'm also interested in reworking the code if necessary. It would be important for the editors that these galleries are per-post, and not separate, through shortcodes like eg. the NextCellent

0

You can use post__not_in to exclude the image by ID:

$images_query_args = array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post__not_in' => array(get_post_thumbnail_id())
);
| improve this answer | |
  • Sorry, but your code doesn't work, it also shows the post-thumbnail and also it has a typo in it (post_not_in). I'll answer the question with the snippet I found on the codex with get_post_thumbnail_id() page (thanks for pointing to this). It is 'exclude', and not 'post_not_in' for filtering – Gas Mar 7 '16 at 7:20
  • sorry, should have been array() around the post thumbnail ID, code otherwise works. exclude ultimately uses post__not_in, either will work. – Milo Mar 7 '16 at 18:37
  • and note that the double underscore is not a typo. – Milo Mar 7 '16 at 20:26
0

This is the correct code, as found on https://codex.wordpress.org/Function_Reference/get_post_thumbnail_id#Show_all_attachments_for_the_current_post_except_the_Featured_Image

<?php

$args = array(
    'post_type'   => 'attachment',
    'numberposts' => -1,
    'post_status' => 'any',
    'post_parent' => $post->ID,
    'exclude'     => get_post_thumbnail_id(),
);

$attachments = get_posts( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title', $attachment->post_title );
        the_attachment_link( $attachment->ID, false );
    }
}

?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.