Skip to content
Advertisement

How can I limit the attached images shown to just one? [closed]

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] . '" />';
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement