简体   繁体   中英

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

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

Try this

global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post'); 
echo $thumb[0];

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

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); 
//

}

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>

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;

 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM