Reputation:
In my blog each post contains 2 images: the featured image + another attachment image.
I'm currently calling the featured image with this code:
echo get_the_post_thumbnail_url();
And then using the following one to try and get the other attachment image:
$images = get_attached_media('image' );
$image = reset($images );
$ximage = wp_get_attachment_image_src($image->ID,'medium');
echo '' .$ximage[0] . '';
The problem is the second code seems to get a) the featured image or b) the other attachment image in a random way.
I would like to edit the second code, setting it to ignore the featured image, so it will always echo the other attachment image in all posts.
Something like:
exclude="' . get_post_thumbnail_id( $post->ID ) . '";
Is it possible?
Alternatively, is it possible to tell the second code to get the most recent uploaded attachment?
Upvotes: 0
Views: 442
Reputation: 11558
This will work in these two conditions:
1). There is a featured image and there is a second image.
2). There is no featured image, but another image
If you have more than one image in the content, this code may need to be modified to account for the other images - however, the key($images)
may just grab the first key it finds.
// Get all the images
$images = get_attached_media('image');
// Get the featured image
$featured_image_id = get_post_thumbnail_id();
// If there is a featured image
if ( has_post_thumbnail() ) {
// Remove the featured image from the images array
unset($images[ $featured_image_id ] );
}
// Set your ximage var to get the src using the key from the $images array - which is the 2nd image ID.
$ximage = wp_get_attachment_image_src( key($images),'medium');
// Echo it out.
echo '<img src="' . $ximage[0] . '">';
Upvotes: 1