Be Studios

Intercontinental Design Firm

This is our knowledge center. Enjoy!

Get first image attached to a post

Useful:

function get_post_image($size = 'thumbnail') {
    global $post;
    $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
    if ($photos) {
        $photo = array_shift($photos);
        return wp_get_attachment_image($photo->ID, $size);
    }
    return false;
}

To print it on the HTML, echo this function with the size (thumbnail, medium or large):

echo get_post_image('medium');