WordPress.org

Ready to get started?Download WordPress

Forums

How do I: Exclude Featured Image when fetching attachments? (5 posts)

  1. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi,

    I'm using this code to fetch all image attachments from each post:

    <?php if ( $images = get_children(array(
    			'post_parent' => get_the_ID(),
    			'post_type' => 'attachment',
    			'post_mime_type' => 'image',
    		))) : ?>
    		<ul>
    		<?php foreach( $images as $image ) :  ?>
    			<li><?php echo wp_get_attachment_link($image->ID, 'thumbnail-latest'); ?></li>
    		<?php endforeach; ?>
    	</ul>
    <?php else: // No images ?>
    	<!-- This post has no attached images -->
    <?php endif; ?>

    For example a Particular post has 3 uploads/attachements 1 x Featured Image and 2 x sub images....

    I want to exclude the featured image so it just shows the other 2 images uploaded...

    How can that be done please?

    Thanks.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Off the top of my head, would this work?

    <?php foreach( $images as $image ) :
    if( wp_get_attachment_link($image->ID, 'thumbnail-latest') != get_the_post_thumbnail( get_the_ID() ) :?>
    <li><?php echo wp_get_attachment_link($image->ID, 'thumbnail-latest'); ?></li>
    <?php endif; endforeach; ?>
  3. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi,

    Thanks for your reply.

    I'm getting this error:

    Parse error: syntax error, unexpected ':' in...

    I've tried removing it, substituting it for a ';'... but no good...

    Any ideas?

  4. Aboobacker P
    Member
    Posted 2 years ago #

    Try, this should work:

    <?php
    	$thumb_ID = get_post_thumbnail_id( $post->ID );
    	if ( $images = get_children(array(
    			'post_parent' => get_the_ID(),
    			'post_type' => 'attachment',
    			'post_mime_type' => 'image',
    			'exclude' => $thumb_ID,
    		))) : ?>
    		<ul>
    		<?php foreach( $images as $image ) :  ?>
    			<li><?php echo wp_get_attachment_link($image->ID, 'thumbnail-latest'); ?></li>
    		<?php endforeach; ?>
    	</ul>
    <?php else: // No images ?>
    	<!-- This post has no attached images -->
    <?php endif; ?>
  5. Aboobacker P
    Member
    Posted 2 years ago #

    Stored featured image id to $thumb_ID and then excluded it from the array of attached images.

Topic Closed

This topic has been closed to new replies.

About this Topic