2016-01-02 72 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,$大小)