2016-01-02 27 views
1

我有一个页面,可能会显示类别的帖子。
我使用此代码如何获得帖子缩略图网址

 <div id="grid" class="grid-container" style="display: block;"> 
    <ul class="grid columns-2"> 
    <?php 
    $args = array(
    'category' => 0, 
    'numberposts' => 9, 
    'post_type' => 'post', 
    'post_status' => 'publish', 
    'suppress_filters' => true); 
    $recent_posts = wp_get_recent_posts($args); 
    foreach($recent_posts as $recent){ 
    echo '<li><a href="' . get_permalink($recent["ID"]) 
    . '" title="'.$recent["post_title"].'" ><img class="aligncenter wp-image-80" src="" alt="'.$recent["post_title"].'"/></a> 
    <h4>'.$recent["post_title"].'</h4></li> '; 
      } 
     ?> 
    </ul> 
</div> 

,问题是,我无法显示的缩略图。
我试图找到如何获得张贴缩略图网址并将其放入它

+0

通行证$最近[ “ID”]像get_the_post_thumbnail($ recent [“ID”],'thumbnail');检查我的答案! –

+0

可能重复[如何获得WordPress发布特色图片url](http://stackoverflow.com/questions/11261883/how-to-get-wordpress-post-featured-image-url) –

+0

这个问题是重复的。答案可以在“http://stackoverflow.com/questions/11261883/how-to-get-wordpress-post-featured-image-url” –

回答

0

通过将帖子ID尝试下面的代码片段。

get_the_post_thumbnail($post_id);     

get_the_post_thumbnail($post_id, 'thumbnail');  // Thumbnail (Note: different to Post 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 

参见网址:
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

+2

这不是正确的答案。 OP请求URL。这个WP函数返回缩略图图像标签。不是网址。 –

1

get_the_post_thumbnail没有正确的答案,因为该函数调用提供了这样的事情:<img src="#">,而不是只用一些URL。

那么,以此为例。

对于我有什么了解你需要得到这个职位缩略图网址,而不是完整的HTML的img对象,这是你可以做到这一点的方式:

$args =array('numberposts' => 1,'post_type' => 'post','order' => 'DESC', 'posts_per_page' => 1); 
$data = query_posts($args); 
$something = NULL; 
for($i=0;$i<count($data);$i++){ 
    $something[$i]['id'] = $data[$i]->ID; 
    $post_thumbnail_id = intval(get_post_thumbnail_id($something[$i]['id'])); 
    $array_thumbnail = wp_get_attachment_image_src($post_thumbnail_id,'medium'); 
    $something[$i]['image_url']=$array_thumbnail[0]; 
    echo $something[$i]['image_url']; 
} 

的$ args =争鸣查询。

$ data =查询结果集。

$ something =您要使用的数组存储要使用的帖子集合的特征图像的url(在这种情况下,只有一个,正如查询参数之一所述) 。

$ something [$ i] ['id'] =您正在使用的每篇文章的ID。

$ post_thumbnail_id =图片的ID设置为媒体库中当前帖子中的精选图片。

$ array_thumbnail =图像的实际网址,你需要,你可以看到这意味着你所得到的HTML的img对象的src值当前设置为当前岗位特色形象。

$ something [$ i] ['image_url'] =你在寻找什么。

- 功能中使用 -

get_post_thumbnail_id($ POST_ID)

wp_get_attachment_image_src($ media_post_id,$大小)