如何在wordpress中使用post id获取post thumbnail?

Man*_*nju 6 php wordpress wordpress-theming wordpress-plugin

我试图使用post_id获取帖子缩略图,但我遇到了很多问题.

我在主题目录中的一个单独的php文件中调用该函数

echo get_the_post_thumbnail('637');
Run Code Online (Sandbox Code Playgroud)

致命错误:在...中调用未定义的函数get_the_post_thumbnail()

1)我们可以使用post_id获取缩略图

要么

2)我们可以使用post_id获取图像源

请任何身体帮助我

提前致谢

小智 11

试试这个

global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post'); 
echo $thumb[0];
Run Code Online (Sandbox Code Playgroud)


vrk*_*ara 8

在你的情况下,你犯了一个小错误,当函数需要一个整数值时,你将单引号放在函数内.

 echo get_the_post_thumbnail('637');  

Bellow代码是有效的尝试.

简单的形式

 echo get_the_post_thumbnail(637);  

Size Specified Form,其中第二个参数是图像的大小.

 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 .我也打算在我的博客上写一篇关于这个主题的完整帖子


rav*_*tel 5

使用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
Run Code Online (Sandbox Code Playgroud)
Out side of loop
Run Code Online (Sandbox Code Playgroud)
global $post;


if (has_post_thumbnail( $post->ID ) ){
//    
      get_the_post_thumbnail($post->ID); 
//

}
Run Code Online (Sandbox Code Playgroud)