I want to display the post-thumbnail image in the header using get_the_post_thumbnail_url() with defined size, however if I am using it without specifying the size, like get_the_post_thumbnail_url() it is displaying the thumbnail img, but if I am using it like: get_the_post_thumbnail_url('thumbnail') nothing is shown.

my code in the header.php is:

function vkb_header_style() {
         if (has_post_thumbnail() and is_singular()) {
             $vkb_thumbnail_url = get_the_post_thumbnail_url('thumbnail');
             echo 'background-image: url(' . $vkb_thumbnail_url . ');';
         } else {

<header id="masthead" class="site-header vkb-header" role="banner" style=" <?php vkb_header_style(); ?>">

The first parameter for get_the_post_thumbnail_url() is the post ID or post object, and not the image size which is actually the second parameter.


// Instead of this:
get_the_post_thumbnail_url( 'thumbnail' )

// You should use:
get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) // pass a post ID
get_the_post_thumbnail_url( get_post(), 'thumbnail' )   // or post object

// Or you can pass a null to use the current post:
get_the_post_thumbnail_url( null, 'thumbnail' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.