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?
If you want display an image that is inserted into your content (a hotlinked image, for example), you must use a function like this (source):
add in functions.php:
Then place
<?php echo catch_that_image() ?>
where you want display the image.Note: a hotlinked image just placed in your content can’t be set as Featured Image, a bultin WordPress’feature.
I suggest two ways:
Using a Plugin
I would consider using the Get The Image plugin, so you could do something like:
The above will try to do things in this order:
Building support in your theme
However, I’m using a function in a plugin that implements the first two items of the list above.
You can adapt it to also match the third item within Diana’s snippet:
Just stick these two functions in your
functions.php
file and use them in the loop like:the code seems perfectly safe. like you said, you don’t have any image attached to the post.
Consider going to the media admin panel and attach a image to that post.
Alternately, scrap the post content with a regex for images in it.
I understand this is very old question, but i am putting my answer here since most voted answer is not appropriate for people who new to PHP.
preg_match is not a good approach for parsing HTML in PHP since preg_match is for regular expression and HTML is not regular expression.
We can use DOM instead.
Using DOM is really good since you can do more thing other than just getting first image and it is right way to parse html.
I wish i can put answer for using wordpress functions (functions from CODEX and core) to get first image but that is also the problem that i am dealing with.
This code works for me:
If you are trying to find an attachment post id with attachment_url_to_postid once you find an image in the content, you may not find that id if the image you found is a scaled version of the image.
If you are not worried about similarly named images, you can replace “-800×600.jpg” with preg_replace:
You can also search the database more manually for the attachment id if you are concerned about misleading file names (e.g. cats-800×600.jpg could be a resize of an old cat image and the current cats.jpg is actually a picture of a goose).