I am using this code directly from the codex.
function echo_first_image ($postID)
{
$args = array(
'numberposts' => 1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
//print_r($attachments);
if ($attachments) {
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );
echo '<img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'" class="current">';
}
}
}
I call it within the loop like this echo_first_image ($post->ID);
The function does call but nothing gets output … as far as I can see there is nothing in $attachments
I do have an image in the post I am using. It’s not a featured image or in a gallery, just in the post.
Am I doing something wrong, or is there something wrong with the code in the first place?