How to Get Featured Image URL in WordPress

How to Get Featured Image URL in WordPress
169 Views

These functions will help you whether you want to show featured images in recent posts or in the single.php file for your theme.

<?php
//Enable theme support for featured images
  add_theme_support( 'post-thumbnails' );
?>

The majority of themes will already have this enabled, but if you’re creating a theme from scratch, you must add theme support for post-thumbnails.

Display Featured Image

using the get_the_post_thumbnail() function built into WordPress to display the featured picture of a post in an <img> tag. This is the simplest way to show the featured image for a post.

<?php
//Displays the featured image in a <img> tag (use this in a loop)
echo get_the_post_thumbnail();
?>

Additionally, you can fill out the second argument if you want to grab a certain size for the featured image.

<?php
//Displays the featured image in a <img> tag resized to the 'medium' thumbnail size (use this in a loop)
echo get_the_post_thumbnail( get_the_ID(), 'medium' );
?>

Get Post Featured Image URL

The get_the_post_thumbnail_url() function returns the URL of the featured post picture if you are running WordPress 4.4 or later (published in 2015).

<?php
//Display the featured post URL (you can replace 'medium' with a different image size)
echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );
?>

This code just displays the featured image’s URL. The code may then be modified to use the post thumbnail URL in any way you like.

You may also change the image’s size. Replace the word’medium’ with another default picture size, such as ‘thumbnail,”medium-large,’ ‘large,’ or ‘full‘.

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.