how to get url ONLY of featured image in wordpress
Solution 1
If your "thumbnail" images are large enough to do what you need:
<?php wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
Otherwise you'll need to go for something like:
<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(300, 300), false, ''); echo $src[0]; ?>
EDIT: The array(300, 300)
bit is the image size you'd like to fetch. WP will substitute any image it has that is at least as large as what you asked for. You can also use 'thumbnail'
, 'medium'
, 'large'
or 'full'
to select one of the pre-configured sizes, as well as any name defined by add_image_size()
in your template or plugins.
Solution 2
First get the id of the featured thumbnail image:
$thumb_id = get_post_meta( $post_id, '_thumbnail_id', true );
One way to do it:
$thumb = get_post($thumb_id);
$thumb_url = $thumb->guid;
Another way would be:
$thumb_url = wp_get_attachment_url( $thumb_id );
Be careful about what is stored in the GUID and what is returned by get_attachment_url
. The attachment url (depending on configuration) can be the page where the attachment is visitable (not the raw file URL but the page where attachment.php
is used).
Adrian
Updated on June 09, 2022Comments
-
Adrian almost 2 years
I'm using timthumb.php and need to be able to get the url of the featured image of a post, when I use functions like
the_post_thumbnail
it does more than just the url.Here is the code so, the capital letters is where I need to insert the exact url, any help would be great, thanks.
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <img src="<?php bloginfo('template_url'); ?>/wp-content/themes/limerickfc/timthumb.php?src=<?php URL OF FEATURED IMAGE ?>&h=760&width=474" alt="" title="<?php the_title(); ?>" /> </a>
-
scottoliver over 11 yearsInstead of setting a size by using the array, you can also use the name for the size of the image that you want (ex. large).
-
smitelli over 11 yearsGood point. I updated my answer with some more info about that.
-
Adrian over 11 yearsThanks , Im having trouble getting timthumb to load anything now, but ill accept the answer when I get it working.
-
smitelli over 11 yearsIf it's any help, your example code shows a discrepancy in that one of the parameters is
h=
and the other iswidth=
. One of those should be eitherheight=
orw=
, depending on what the library requires. Also,&
should generally be written as&
in HTML.