how to get post thumbnail using post id in wordpress?

I am trying to get the post thumbnail using post_id,but i am getting so many problems.

Iam calling the function in a separate php file in theme directory

Read More
echo get_the_post_thumbnail('637');

Fatal error: Call to undefined function get_the_post_thumbnail() in …

1)can we get the thumbnail using post_id

or

2)can we get the image source using post_id

please any body help me

Thanks in advance

Related posts

Leave a Reply

5 comments

  1. In your case you make a small mistake that you put the single quote inside the function when function require an integer value.

     echo get_the_post_thumbnail('637');  

    Bellow code are valid try it.

    Simple Form

     echo get_the_post_thumbnail(637);  

    Size Specified Form where second argument is the size of the image.

     echo get_the_post_thumbnail(637, array(100,100));  

    also you can try bellow code also

    get_the_post_thumbnail(637);                  // without parameter -> Thumbnail
    get_the_post_thumbnail(637, 'thumbnail');     // Thumbnail
    get_the_post_thumbnail(637, 'medium');        // Medium resolution
    get_the_post_thumbnail(637, 'large');         // Large resolution
    get_the_post_thumbnail(637, 'full');          // Original resolution
    

    Also you can refer to the WordPress codex Here.
    I am also going to write a full post on this topic on my blog

  2. Use Require_once Or include_once

    require_once('/the/path/to/your/wp-blog-header.php');
    
    include_once('wp-blog-header.php' );
    
    
    
    
    
    get_the_post_thumbnail($post_id);           // without parameter -> Thumbnail
    
    
    get_the_post_thumbnail($post_id, 'thumbnail');     // Thumbnail
    get_the_post_thumbnail($post_id, 'medium');        // Medium resolution
    get_the_post_thumbnail($post_id, 'large');         // Large resolution
    get_the_post_thumbnail($post_id, 'full');          // Original resolution
    
    get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions
    
    Out side of loop
    
    global $post;
    
    
    if (has_post_thumbnail( $post->ID ) ){
    //    
          get_the_post_thumbnail($post->ID); 
    //
    
    }
    
  3. Vallabh’s solution works. This is how I use it as a background image:

    <?php if (has_post_thumbnail( $post->ID ) ) {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id(637), 'thumbnail' );
        $image = $image[0];
    } ?>
    
    <div style="background-image: url(<?php echo $image; ?>)"> ... </div>
    
  4. Create a post template..look like this(post_temp.php)

     <?php
    
       $args=array('order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
    
       $query=new WP_Query($args);
    
       if( $query->have_posts()): 
    
       while( $query->have_posts()): $query->the_post();
    
       {
         echo get_the_post_thumbnail($post->ID); 
       }
    
       endwhile; 
       else:
       endif;
    
     ?>