get_the_post_thumbnail_url() not working when adding an image size
Asked Answered
C

2

8

I have a weird situation when I am trying to use get_the_post_thumbnail_url() in Wordpress.

To be more precise, I have this variable:

$postImage = get_the_post_thumbnail_url('article-thumbnail-image');

and it returns a blank value. If I use:

$postImage = get_the_post_thumbnail_url();

the value returned is correct but it has a different image size (150x150). Now the weird part is if I use:

the_post_thumbnail_url('article-thumbnail-image');

it shows the right value as this image size is registered in functions.php but I need to use the GET function.

Am I doing something wrong?

Carriole answered 12/2, 2017 at 14:27 Comment(0)
M
9

There are two arguments in get_the_post_thumbnail_url(). You'll need to either use null or the post_id/post for the first argument and the size would be the second. https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

Marmite answered 12/2, 2017 at 14:38 Comment(1)
$postImage = get_the_post_thumbnail_url('', 'article-thumbnail-image'); that worked.Uxorious
F
0

if post_id == null then you can use this function.

the_post_thumbnail_url( string|int[] $size = ‘post-thumbnail’ )

sample:

$postImage = the_post_thumbnail_url('article-thumbnail-image');

reference:

https://developer.wordpress.org/reference/functions/the_post_thumbnail_url/

Faline answered 15/7 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.