php 如何在wordpress中使用帖子ID获取帖子缩略图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21754745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get post thumbnail using post id in wordpress?
提问by Manju
I am trying to get the post thumbnail using post_id,but i am getting so many problems.
我正在尝试使用 post_id 获取帖子缩略图,但是我遇到了很多问题。
Iam calling the function in a separate php file in theme directory
我在主题目录中的单独 php 文件中调用该函数
echo get_the_post_thumbnail('637');
Fatal error: Call to undefined function get_the_post_thumbnail() in ...
致命错误:调用未定义的函数 get_the_post_thumbnail() in ...
1)can we get the thumbnail using post_id
1)我们可以使用 post_id 获取缩略图吗
or
或者
2)can we get the image source using post_id
2)我们可以使用post_id获取图像源吗
please any body help me
请任何机构帮助我
Thanks in advance
提前致谢
回答by brunomarks7
Try this
尝试这个
global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post');
echo $thumb[0];
回答by vrkansagara
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.
Size Specified Form 其中第二个参数是图像的大小。
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
回答by ravi patel
Use Require_once Or include_once
使用 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
Out side of loop
global $post;
if (has_post_thumbnail( $post->ID ) ){
//
get_the_post_thumbnail($post->ID);
//
}
回答by Henning Fischer
Vallabh's solution works. This is how I use it as a background image:
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>
回答by RichestSoft
Create a post template..look like this(post_temp.php)
创建一个帖子模板..看起来像这样(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;
?>

