Show only first image of multiple image field

Below are four examples of getting a list of items, returning the first and showing a single image from the first id.


explode

// get your custom field data (this looks like your meta_value)

$meta_value = "378,377,376,375,374,373"; 

// convert csv to array

$ids = explode ( ',', $meta_value );

// pull the first item out as the variable $id

list($id) = $ids;

// show the image

echo wp_get_attachment_image ( $id, 'medium' );

get_attached_media

// assume you have these vals
$post = get_post();
$post_id = $post->ID;

// get the attachments
$attachments = get_attached_media('image', $post_id);
if(count($attachments) > 0) {
    $first_attachment = array_shift($attachments);
    echo wp_get_attachment_image($first_attachment->ID);
}

get_post_meta

$post = get_post();
$post_id = $post->ID;
$key = 'attachment';

$generate_if_not_found = false; // for testing
if($generate_if_not_found && empty(get_post_meta($post_id, $key, true))) {
    // write the values if we don't have them
    update_post_meta($post_id, $key, array(386, 393, 234, 23, 23, 234));
}

$ids = get_post_meta($post_id, $key, true);
if( ! empty($ids) && count($ids) > 0) {
    $first_id = array_shift($ids);
    echo wp_get_attachment_image($first_id);
}

get_post_custom_values

$post = get_post();
$post_id = $post->ID;
$key = 'attachment';

if(is_array($value = get_post_custom_values($key, $post_id))) {
    if(count($value) > 0) {
        if(is_array($ids = maybe_unserialize($value[ 0 ]))) {
            if(count($ids) > 0) {
                $id = array_shift($ids);
                echo wp_get_attachment_image($id);
            }
        }
    }
}

Custom Column

function posts_render_custom_columns($column_name, $post_id) {
    if($column_name === 'attachment') {

        $size="thumbnail";

        $attachments = get_attached_media('image', $post_id);
        if(count($attachments) > 0) {
            $first_attachment = array_shift($attachments);
            echo wp_get_attachment_image($first_attachment->ID, $size);
        }
    }
}