I have the URL of my image and I want to display the thumbnail of that image. Is this possible. I check the_post_thumbnail() function but I can't add the URL of the image as an input.
1 Answer
Get the image ID from the URL with attachment_url_to_postid():
$image_id = attachment_url_to_postid( $image_url );
Convert the ID to the thumbnail image URL with wp_get_attachment_image_src:
$thumbnail_url = wp_get_attachment_image_src( $image_id, 'thumbnail' );
-
1I was just about to test the function using a featured image URL. Did you try this? At any rate, those "url to ID" functions are resource heavy and best avoided if possible.s_ha_dum– s_ha_dum2016-01-08 15:59:38 +00:00Commented Jan 8, 2016 at 15:59
-
You should not post bare code. A good answer contains verbal explanation as well. Not my downvote, but I am pretty sure that is the "why" of the vote.s_ha_dum– s_ha_dum2016-01-08 16:37:56 +00:00Commented Jan 8, 2016 at 16:37
-
Either another user or the system has flagged this as low quality (see flag). Please stop dumping code, to be really honest, this is not the first low quality flag I handle on your of your post. I'm really not after you as you have previously claimed, but I cannot overlook low quality flags in review queue. Add proper explanation to your code to avoid your posts being auto-flagged or being flagged by other usersPieter Goosen– Pieter Goosen2016-01-08 16:41:52 +00:00Commented Jan 8, 2016 at 16:41
-
I'm almost certain that single code blocks, like it was originally, are getting always flagged as low quality. So it is better to do the explanation not as code comment, but as regular text.Nicolai Grossherr– Nicolai Grossherr2016-01-08 16:59:22 +00:00Commented Jan 8, 2016 at 16:59
-
That is an improvement. Links to the Codex page for the relevant functions are always a good thing. caveats are helpful-- "not tested but...", "this might break if..."s_ha_dum– s_ha_dum2016-01-08 17:10:11 +00:00Commented Jan 8, 2016 at 17:10
var_dump($test_wp['no_image_post_IMG']);?