2013-10-16 27 views
1

我想在我的主頁上添加「本週圖片」功能。我設置了它,所以我創建了一個帖子類別「本週圖片」,我將發佈帖子並將圖片添加爲特色圖片。我正在尋找PHP來返回最後發佈在特定類別中的最後一個特色圖片。如何返回Wordpress中最新發布的精選圖片?

我真的不知道從哪裏開始,所以我很抱歉沒有把我試過的東西。到目前爲止,我已經找到

<?php the_post_thumbnail(); ?> 

回答

2

您可以(使用它的id獲得通過category name廣告帖子的最後再拿到特色圖像)試試這個

$args = array(
    'category_name' => 'Pic of the Week', 
    'posts_per_page' => 1, 
    'order_by' => 'date', 
    'order' => 'desc' 
); 

$post = get_posts($args); 
if($post) { 
    $post_id = $post[0]->ID; 
    if(has_post_thumbnail($post_id)){ 
     // use one of these 
     echo get_the_post_thumbnail($page->ID, 'thumbnail'); 
     echo get_the_post_thumbnail($post_id, array(80, 80), array('class' => 'post_thumbnail')); 
    } 
} 
+0

非常感謝!知道如何在該代碼中指定圖像的大小尺寸? –

+0

歡迎和這個'數組(80,80)'是維度。 –

+0

嗯由於某種原因,圖片是巨大的 - 就像幾乎佔據了整個頁面。我改變了尺寸較低,但我不認爲這是問題。爲了避免陷入某些div,我在代碼中移動了它,但它很大。有任何想法嗎? –

2

首先,我會在帖子ID使用get get recent posts function

wp_get_recent_posts($args, $output) 

這將允許您獲得帖子ID。然後,您可以使用後續來獲取圖像。

$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
+0

'OP'想要在'Pic of the Week'類別下獲得最近發佈的縮略圖。 –

+0

這就是$ args在wp_get_recent_posts中的作用。上面選擇的答案與我的答案非常相似。 –

相關問題