如何在WordPress中使用帖子ID获取帖子缩略图?

我正在尝试使用post_id获取帖子缩略图, 但是我遇到了很多问题。

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

echo get_the_post_thumbnail('637');

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

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

or

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

请任何人帮助我

提前致谢


#1


尝试这个

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

#2


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

 echo get_the_post_thumbnail('637');  

波纹管代码是有效的尝试一下。

简单的形式

 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。我还将在我的博客上撰写有关此主题的完整文章


#3


使用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

循环外

global $post;


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

}

#4


Vallabh的解决方案有效。这就是我将其用作背景图像的方式:

<?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>

#5


创建一个帖子模板..看起来像这样(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;

 ?>

来源:

https://www.srcmini02.com/66555.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37070.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?