Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:
add_theme_support('post-thumbnails');
To a theme’s functions.php file and you’ll get a Featured Image module on the admin screen for posts which allows you to select one.
It is also very easy to output that image as an HTML <img>
:
get_the_post_thumbnail();
But what if you just need the URL? Say, you’re going to use it as a background-image
on an element rather than a content image. Unfortunately there is no super easy/obvious function for that.
Within the loop, you’ll have to do:
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
Then $thumb_url
will be that URL.
You can also use this code, especially when storing posts in an array:
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID));
Saved my butt. Thanks man.
I am new both WordPress and php so I purchased a theme for my site to simplify things. I have been making changes to the site and was struggling for hours on getting the post attachment url. My problem was that I was looking through the theme functions and files. Oh well. you live and you learn. Thanks again.
Aaron, how would you pass the thumbnail size in your code? I tried it, and it shows the url of the original image.
Hi Ben, in that case you can use this:
wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size, $icon );
You can set a registered size on the second parameter…
Thanks, so much more elegant, everything else I was coming across was so convoluted.
Fantastic.Very easy.Thank you.
Hey Chris, i’m a little stumped on a task with trying to get the featured image source of the next and previous post. i know how to get the featured image of the next and previous posts, im using them for pretty Next/Prev nav buttons, but im also using featured video plus plugin in my theme, so it auto replaces the image with the featured video. excellent plugin, and ive got just the video thumbnail to work on my blog and cat pages by grabbing the img src rather than post_thumbnail, but still stumped on how to do this for Next/Prev navigation on single posts. Any tips you can point out for me?
Thanx! :)
Hi. I am trying to achieve this in one go. But I am not very familiar with PHP.
I would like to print the complete style with PHP and fetch featured image. Tried something like this inside a simple WP plugin I’v made:
But it didn’t work and when debugging without the WP commands I’v also noticed that the script printed its result at the first line in the document. Preferably I would like the output to show up inside .
Any help would be very appreciated!
One Liner ~
<?= wp_get_attachment_image_src( get_post_thumbnail_id(), 'full', false )[0] ?>
Amazing! Thanks Edward
Exactly what I was looking for! Your posts constantly get me out of jams. Big Thanks!!
Hi all,
I need some help please, I’m trying to add a featured image as a background in post that is in a page (confusing I know).
All the tutorials I can find only allows me to add it when the post is a “page” but I’ve got some code already that pulls through an specific post into a page and want the background in the div to be the featured image but can’t find anywhere that will show me how!
Can anyone help
Hi Sam,
you can use this method.
to get featured image as background image,
insert this code within the loop
and create a div ex. banner
ID); //get the id of the featured image
$featuredImage = wp_get_attachment_image_src($imgID, 'full' );//get the url of the featured image (returns an array)
$imgURL = $featuredImage[0]; //get the url of the image out of the array
?>
.banner {
background:url('') no-repeat top center #DA6F4F;
min-height: 100px;
}
getting error in uploading the code properly
<?php
if (has_post_thumbnail()) { //if a thumbnail has been set
$imgID = get_post_thumbnail_id($post->ID); //get the id of the featured image
follow with the continued reply…
Hi Sam,
You can use this method
thank you for your code it works
http://www.gazianteptasarim.com
Has all the solution! :)
Hi! But, where should I put that code? I’m using Post Type Builder plugin which allows me to add custom posts and edit archive and template layout, so, I want to set the feature image as a Heading Background and the Title of the post right in the center of that image… How can I do that possible?? :(
Thanks a lot for this!
Just do get_the_post_thumbnail_url();
Can any someone advice to me, how to set this fix URL as Automatically Featured Image
This my image url:
Hi Bung Nas,
Sounds like you’re trying to automatically set a featured image. That’s a little bit different than this snippet, which provides a method for fetching the URL of post’s featured image.
You might want to look into a plugin that automatically sets the featured image for you. I haven’t tried this one personally, but it looks promising:
https://wordpress.org/plugins/wp-auto-featured-image/
Cheers!
I am in the process of changing themes and have many past blog posts with a url to the featured image. In the past I have not uploaded the image to the library, instead I just linked to a file on the server – ex: domain.com/Blog_Photos/2018/4802_haze.jpg. I was able to reference the url in the post when I clicked the add media icon. Now that I am changing themes, it will not automatically grab the url and use the photo. The blog list does not show the image unless it is in the media library. Is there any code I can use to grab the url that is currently in the context of the text? Thanks!
I am not having the featured image url as I have pasted this code in template.php. I have seen the code on this article https://www.wpblog.com/wordpress-get-featured-image-url/
ID) );
?>
<div class="wpblog-featured-image"
style=”wpblog-featured-background-image: url();”
>
Hi Amanda! You might have more luck using the standard function for the featured image URL.
I haven’t tested this, but should be something like:
Now use get_the_post_thumbnail_url();
Hey! You can also use get_the_post_thumbnail_url() function