Get the src Attribute from WordPress Post Thumbnail
If you want to retrieve the src
attribute (the image URL) from a WordPress post thumbnail (featured image), there are straightforward ways to do it. This is useful when you need to use the image URL directly in your theme templates, custom blocks, or plugins instead of the full image HTML markup.
This tutorial explains how to quickly get the src
attribute from the post thumbnail, why it works this way, and provides practical code snippets to implement it correctly.
Quick Fix
- Use
get_the_post_thumbnail_url( $post_id, $size )
to get the image URL directly. - If you only have the post thumbnail HTML, use
wp_get_attachment_image_src()
with the thumbnail ID. - Output the URL in your template or assign it to a variable for further use.
Why This Happens
WordPress post thumbnails are stored as attachment IDs linked to posts. The function the_post_thumbnail()
outputs the full <img>
tag, not just the URL. To get the src
attribute, you need to retrieve the image URL separately using dedicated functions.
Directly extracting the src
from the HTML string is unreliable and inefficient. WordPress provides functions that return the image URL or image data array, which is the correct approach.
Step-by-step
- Get the post thumbnail URL for the current post:
$thumbnail_url = get_the_post_thumbnail_url( get_the_ID(), 'full' ); if ( $thumbnail_url ) { echo 'Thumbnail URL: ' . esc_url( $thumbnail_url ); } else { echo 'No thumbnail set for this post.'; }
- Get the post thumbnail URL for a specific post ID:
$post_id = 123; // Replace with your post ID $thumbnail_url = get_the_post_thumbnail_url( $post_id, 'medium' ); if ( $thumbnail_url ) { echo 'Thumbnail URL: ' . esc_url( $thumbnail_url ); } else { echo 'No thumbnail set for this post.'; }
- Get the image URL from the attachment ID:
$thumbnail_id = get_post_thumbnail_id( $post_id ); $image_src_array = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' ); if ( $image_src_array ) { $image_url = $image_src_array[0]; echo 'Image URL: ' . esc_url( $image_url ); } else { echo 'No image found.'; }
- Use the URL in an <img> tag manually:
if ( $thumbnail_url ) { echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="Post Thumbnail">'; }
Template Tags & Functions
Function | Description | Returns |
---|---|---|
get_the_post_thumbnail_url( $post_id, $size ) |
Returns the URL of the post thumbnail image for a given post ID and size. | String (image URL) or false if no thumbnail set. |
get_post_thumbnail_id( $post_id ) |
Returns the attachment ID of the post thumbnail. | Integer (attachment ID) or 0 if no thumbnail. |
wp_get_attachment_image_src( $attachment_id, $size ) |
Returns an array with image URL, width, height, and whether it is intermediate size. | Array or false if no image found. |
the_post_thumbnail( $size, $attr ) |
Echoes the full <img> tag for the post thumbnail. | Outputs HTML directly. |
Code Snippets
Below are reusable code snippets you can copy and paste into your theme files or custom plugins.
<?php
// Get current post thumbnail URL (full size)
$thumbnail_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );
if ( $thumbnail_url ) {
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="' . esc_attr( get_the_title() ) . '">';
} else {
echo 'No thumbnail available.';
}
?>
<?php
// Get thumbnail URL for a specific post ID and size
$post_id = 42;
$size = 'medium';
$thumbnail_url = get_the_post_thumbnail_url( $post_id, $size );
if ( $thumbnail_url ) {
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="Thumbnail for post ' . $post_id . '">';
} else {
echo 'No thumbnail for post ID ' . $post_id;
}
?>
<?php
// Get image URL from attachment ID
$post_id = get_the_ID();
$thumbnail_id = get_post_thumbnail_id( $post_id );
$image_data = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
if ( $image_data ) {
$image_url = $image_data[0];
echo '<img src="' . esc_url( $image_url ) . '" alt="Thumbnail">';
} else {
echo 'No image found.';
}
?>
Common Pitfalls
- Using
the_post_thumbnail()
expecting a URL: This function outputs the full <img> tag, not just the URL. - Not checking if the post has a thumbnail: Always check if the thumbnail exists to avoid errors or broken images.
- Using wrong image size strings: Use valid image sizes registered in your theme or WordPress defaults like
'thumbnail'
,'medium'
,'large'
, or'full'
. - Escaping output: Always use