2014-02-13 56 views
4

我想使用post_id获得发布缩略图,但我遇到了这么多问题。如何在wordpress中使用post id获得发布缩略图?

荫调用主题目录中的一个单独的PHP文件中的函数

echo get_the_post_thumbnail('637'); 

Fatal error: Call to undefined function get_the_post_thumbnail() in ...

1),我们可以得到使用缩略图POST_ID

2)我们可以得到使用图像源post_id

请任何机构帮我

在此先感谢

+0

如果你调用这个函数? WP之外? – Rikesh

+0

我在调用主题目录 – Manju

+0

的单独php文件中的函数是否意味着您在模板文件中调用它...?你会发布一些片段吗? – Dinesh

回答

4

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

} 
+0

我尝试过,但它不起作用。 – Manju

+0

use include_once('wp-blog-header.php'); –

8

在你的情况,你让你把单引号的功能,里面的小错误,当函数需要一个整数值。

 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的法典Here。 我也打算写关于这个问题的全日制岗位上我blog

10

试试这个

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

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

像这样(post_temp.php)创建帖子template..look

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

?>