WordPress.org

Ready to get started?Download WordPress

Forums

Get images attached to posts (no problem) but need to remove the featured image (7 posts)

  1. scdwb
    Member
    Posted 3 years ago #

    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.

  2. scdwb
    Member
    Posted 3 years ago #

    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

  3. cehwitham
    Member
    Posted 3 years ago #

    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.

  4. scdwb
    Member
    Posted 3 years ago #

    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 :-)

  5. c2foryou
    Member
    Posted 3 years ago #

    Great solution, thanks scdwb.

  6. cehwitham
    Member
    Posted 2 years ago #

    Thanks scdwb. Very useful bit of code.

  7. rhossain
    Member
    Posted 2 years ago #

    Thanks Mr. Scdwb.....helped a lot

Topic Closed

This topic has been closed to new replies.

About this Topic