I am trying to fetch the uploaded images using the link URL (The Link URL option is available in the Add Media when we upload/insert media) and display it on web.
I have gone through the methods has_post_thumbnail
and the_post_thumbnail
which are working perfect for me. Now, i am trying to display images (other than featured image) with certain sizing options (as available in the_post_thumbnail
).
I am using the Link URL associated with the uploaded image with the help of the function $im =imagecreatefromjpeg($url[image]);
and imagejpeg($im);
to display it on web but all it displays it is special characters !
Is there a way to do it ?
Update code for fetching a single image from media library:
$media_query = new WP_Query
(
array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_title' => $url[image]
)
);
foreach ($media_query->posts as $post)
{
print_r(wp_get_attachment_image($post->ID));
$imageFound = true;
break;
}
$url[image]
contains the post_title
. The problem is this query is fetching all the images from the media library ! Am i missing something ?
wp_get_attachment_image_src
?get_posts
function to fetch attachment IDs for use withwp_get_attachment_image_src
.post_title
say). So i am using 'WP_Query' to fetch the image from DB. The result though is fetching all images from the media !! Can you suggest something ?post_title
is actually recognized in DB byname
attribute.I changed my query accordingly and it fetches only one image as expected. Thanks!