How to get message thumbnail using id message in wordpress?

I am trying to get a thumbnail of a post using post_id, but I have so many problems.

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

echo get_the_post_thumbnail('637');

Fatal error: function call undefined get_the_post_thumbnail () in ...

1) can we get a sketch using post_id

or

2) can we get the image source using post_id

please, any body will help me

Thank you in advance

+4
source share
5 answers

try it

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

, , .

 echo get_the_post_thumbnail('637');  

Bellow.

 echo get_the_post_thumbnail(637);  

, .

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

.

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

WordPress codex . .

+8

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

}
+4

Vallabha'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>
0
source

Create a post..look template 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;

 ?>
0
source

Source: https://habr.com/ru/post/1526739/


All Articles