wordpress

get_the_post_thumbnail_url() not working when adding an image size


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?


Solution

  • 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/