7

I am using WordPress 3.4.1. I need to display the featured image of a page. How can I get the featured image URL by particular page ID. Any help?

2 Answers 2

24

Did you try anything? Its always helpful to share what you have tried.

$url = wp_get_attachment_url( get_post_thumbnail_id($post_id) );

Or if you want to get the image by image size.

$src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'thumbnail_size' );
$url = $src[0];

http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id

http://codex.wordpress.org/Function_Reference/wp_get_attachment_url

http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

2
  • Actually, this is wrong - it suggests that you can get pass the size as a parameter, but that's not accurate. Neither function accepts a parameter. Commented Jun 20, 2014 at 3:35
  • You can use wp_get_attachment_image_url() instead of wp_get_attachment_image_src() nowadays so you don't have to do that $src[0] part.
    – swissspidy
    Commented Jan 17, 2017 at 19:44
7

As of Wordpress 4.4.0, we can use get_the_post_thumbnail_url():

$url = get_the_post_thumbnail_url( $post_id, 'thumbnail' );
1
  • 1
    Finally, it was about the time.
    – prosti
    Commented Jan 17, 2017 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.