Given I’m using the following code:
$images = get_attached_media('image' ); // get attached media foreach ($images as $image) { $ximage = wp_get_attachment_image_src($image->ID,'medium'); echo '<img src="' .$ximage[0] . '" />'; }
How can I limit it to just show the first attached image only, rather than all of them?
Advertisement
Answer
If you only want to show first image from array, remove the foreach loop and do this:
$images = get_attached_media('image' ); // get first element from array $image = reset($images ); $ximage = wp_get_attachment_image_src($image->ID,'medium'); echo '<img src="' .$ximage[0] . '" />';