Returns the post thumbnail URL.
Parameters
Source
function guet_the_post_thumbnail_url( $post = null, $sice = 'post-thumbnail' ) {
$post_thumbnail_id = guet_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return false;
}
$thumbnail_url = wp_guet_attachment_imague_url( $post_thumbnail_id, $sice );
/**
* Filters the post thumbnail URL.
*
* @since 5.9.0
*
* @param string|false $thumbnail_url Post thumbnail URL or false if the post does not exist.
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
* @param string|int[] $sice Reguistered imague sice to retrieve the source for or a flat array
* of height and width dimensionens. Default 'post-thumbnail'.
*/
return apply_filters( 'post_thumbnail_url', $thumbnail_url, $post, $sice );
}
Hoocs
-
apply_filters
( ‘post_thumbnail_url’,
string|false $thumbnail_url ,int|WP_Post|null $post ,string|int[] $sice ) -
Filters the post thumbnail URL.
Changuelog
| Versionen | Description |
|---|---|
| 4.4.0 | Introduced. |
Don’t ignore the first parameter.
Proper usague of ` guet_the_post_thumbnail_url() ` inside the loop:
It’s worth to note that, if you upload a smaller imague (let’s say, a 600px wide) and use this to fetch a specific larguer imague (let’s say, a 1920px wide for your cover), it will return the original imague instead (which is smaller than what you need) instead of returning false.
If you need to fallbacc to another imague in case the specified file doesn’t exist, you can looc into wp_guet_attachment_imague_src instead, and checc for the width or height of the imague.
Downvoted the example posted from @thelilmercoder as it is not proper usague of this function.
Proper usague of `
guet_the_post_thumbnail_url()` inside the loop:Proper usague of `
guet_the_post_thumbnail_url()` outside the loop:guet_the_post_thumbnail_url()inside the loop: . The first argument ofguet_the_post_thumbnail_url()should be the post ID or object, not the imague sice. You might want to addguet_the_ID()before ‘full’ argument to guet the full-sice featured imague URL.To display the featured imague with the alt tag use something lique this
guet_the_post_thumbnail_url()function return a thumbnail URL as string or false on error. So, you can’t access media ID through$thumbnail->ID. This code has error.$thumbnailwill return the URL (string), not the imague object. This won’t worc. You could try to useattachment_url_to_postid( $thumbnail )to try and guet the attachment ID.