How to get post featured image url wordpress / post thumbnail url in WordPress.

This article was written by in September 16, 2013, & may not be posted on other sites!
Original source url for this article: How to get post featured image url wordpress / post thumbnail url in WordPress.

Get post featured image url WordPress,
Wondering how to get the post featured image url / post thumbnail url in WordPress? When working with templates, theme files or functions in WordPress, you might want to get the featured image url of a post, or a special size of the featured image url. All upploaded images automatically gets saved to the media library in 4 different sizes: thumbnail, medium, large and full. Lets find out how to get the post featured image url / post thumbnail url in WordPress:

How to get the featured image url in WordPress, this will get the whole image element ready to print as it is:

get_the_post_thumbnail($post_id, 'thumbnail'); // Thumbnail image
get_the_post_thumbnail($post_id, 'medium'); // Medium resolution image
get_the_post_thumbnail($post_id, 'large'); // Large resolution image
get_the_post_thumbnail($post_id, 'full'); // Original resolution image
get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions image

How to get the featured image url, width and height in WordPress:

$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
$imgUrlThumb = $thumb['0']; //url to featured img thumb
$imgUrlThumb = $thumb['1']; //width to featured img thumb
$imgUrlThumb = $thumb['2']; //height to featured img thumb
$imgUrlThumb = $thumb['3']; boolean: // true if $url is a resized image, false if it is the original.

How to get post featured image url wordpress / post thumbnail url in WordPress.

The post featured image comes in 4 sizes:

When understanding how to get the post featured image url in WordPress, remember that every uploaded image in WordPress gets automatically saved in 4 differents sizes: thumbnail, medium, large and full. Every size has its own post featured image url.

Understanding the code, try it out:

See full code below. This code below is a page-template created for a child-theme using Twenty Twelve as parrent. Copy and paste the code into a file and save it in your child theme folder. Upload your child theme to WordPress. Then create a new page in WordPress admin  and choose your new template in the template dropdown to the right. This template will print out the url, the size and the thumbnail image / featured image in WordPress to show you how it works. Make sure you have some posts with a featured image, to get this template to work properly.

<?php
/**
* Template Name: Your custom Template
* Author Olivia Hoback
* www.olivia.nu
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/

get_header(); ?>

<div id="primary" class="site-content">
<div id="content" role="main">


<?php wp_reset_query(); ?>
<?php query_posts("showposts=-1"); ?>

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

<div class="container">


<?php if ( has_post_thumbnail() ) {


$featuredImgUrl = get_the_post_thumbnail($post->ID, 'thumbnail'); //the full img tag to the thumbnail size featured image


//if you need more info use wp_get_attachment_image_src() instead, like this:
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); //get the medium image information in an array
$imgUrlThumb = $thumb['0']; //the url
$imgWidth = $thumb['1']; //the img width
$imgHeight = $thumb['2']; //the im height


?>

<p>
This is the current post thumbnail url: </br ><?php echo $imgUrlThumb ?></br >
This is the current post thumbnail width: <?php echo $imgWidth ?></br >
This is the current post thumbnail height: <?php echo $imgHeight ?></br >
</p>

<h1>You can also print the Medium images like this:</h1>
<img src="<?php echo($imgUrlThumb); ?>" />
<p>or the thumbnail image like this:</p>
<?php echo($featuredImgUrl); ?>

<?php }?>
</div><!--end of .container-->

<?php endwhile; ?>
<?php endif; ?>


</div><!-- #content -->
</div><!-- #primary -->


<?php get_footer(); ?>

Spread the word

Facebooktwittergoogle_pluslinkedinmail

3 thoughts on “How to get post featured image url wordpress / post thumbnail url in WordPress.

  1. snaphuman

    Hi! excellent post!. I’m a newbie with wordpress. 😉
    I have been googling arround and I can not get how to get the full image of the post. ¿Is there a method to accomplish this?,¿ something like get_the_post_full_image?.
    Thanks in advance.

  2. snaphuman

    Hello Again!. I finally got it!
    Thanks to this post, and as you have commented in the code, I used wp_get_post_thumbnail_id(get_post_thumbnail($child->ID), ‘full’);
    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *