8

I am currently using the following code to get the URL of the featured image of a wordpress post:

URL="<?php if (function_exists('wp_get_attachment_thumb_url')) {echo wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID), 'big-size'); }?>"

But the code only returns the smaller (150x150px) thumbnail. This is what I get:

http://sitename.com/wp-content/uploads/imagename-150x150.png

My question is, how do I get it to return the URL for the original image (full sized image) which would be:

http://sitename.com/wp-content/uploads/imagename.png

Many thanks for your time and help.

21

There are four valid sizes built in to the WordPress core.

the_post_thumbnail('thumbnail');    // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');       // Medium resolution (default 300px x 300px max)
the_post_thumbnail('medium_large'); // Medium Large resolution (default 768px x 0(means automatic height by ratio) max) since WP version 4.4
the_post_thumbnail('large');        // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');         // Original image resolution (unmodified)

The last is one you're looking for.

The following returns the URL.

<?php
  $src = wp_get_attachment_image_src( $attachment_id, $size, $icon );
  echo $src[0];

The whole code can look like that:

<?php
  $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );
  echo $src[0]; // the url of featured image

More information can be found here.

  • Thanks for your answer. I am actually looking for a function to return the "URL" of the image and not the image itself. So I doubt if the_post_thumbnail is going to work for that. Or perhaps I am wrong? – theshorttree Nov 3 '14 at 17:32
  • @theshorttree see my updated answer. – SLH Nov 3 '14 at 17:34
  • That totally worked, thanks a lot for your time and reply! – theshorttree Nov 3 '14 at 17:56
1

A bit late to the party,

but

get_the_post_thumbnail_url(null,'full'); does exactly the job, where full can be replaced by thumbnail, medium, medium_large or large.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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