Support » Plugins » Get images attached to posts (no problem) but need to remove the featured image

  • Hello,

    I’m using the following code to get the images attached to a post and displaying them in a javascript slider:

    if ( $images = get_posts(array(
    		'post_parent' => $post->ID,
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$attachmenturl=wp_get_attachment_url($image->ID);
    			$attachmentimage=wp_get_attachment_image_src( $image->ID, full );
    			$imageDescription = apply_filters( 'the_description' , $image->post_content );
    			$imageTitle = apply_filters( 'the_title' , $image->post_title );
    
    			if (!empty($imageDescription)) {
        echo '<a href="'.$imageDescription .'"><img src="' . $attachmentimage[0] . '" alt=""  /></a>';
    } else { echo '<img src="' . $attachmentimage[0] . '" alt="" />'; }
    		}
    	} else {
    		echo "No Image";
    	}

    This works perfectly, except that all images are retrieved from the post. I need to retrieve all images except the image I set as the featured image.

    The reason being is because a customer of mine will be uploading the featured image as a different size and ratio to the other images and we can’t have the featured image in the slider.

    Is there a method of being able not to retrieve a specific image when retrieving images attached to a post using the code above?

    Many thanks in advance for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter scdwb

    (@scdwb)

    Is there any chance some one can help me with the above as I’m still struggling to fine a solution with this one.

    Much thanks

    I am doing something similar for a gallery rather than a slider but I want to keep the featured image separate for use elsewhere.

    I’ve not found a solution but will post if I do.

    Thread Starter scdwb

    (@scdwb)

    Hi cehwitham,

    I found a solution and I hope this solves your issue too.

    It was a little hard to find but there is an inbuilt function to find the ID of the featured image attached to a post:

    get_post_thumbnail_id( $post->ID );

    So I assigned this to a variable and then excluded this image in the “get_posts” function that I called into the slide show:

    $thumb_ID = get_post_thumbnail_id( $post->ID );
    
    if ( $images = get_posts(array(
    		'post_parent' => $post->ID,
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'orderby'        => 'title',
    		'order'           => 'ASC',
    		'post_mime_type' => 'image',
    		'exclude' => $thumb_ID,
    		)))
    	{
    		foreach( $images as $image ) {......rest of code

    Hope this helps 🙂

    Great solution, thanks scdwb.

    Thanks scdwb. Very useful bit of code.

    Thanks Mr. Scdwb…..helped a lot

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get images attached to posts (no problem) but need to remove the featured image’ is closed to new replies.