2011-07-29 66 views
3

我想顯示帖子格式的所有帖子(讓我們說:放在一邊)。在WordPress中顯示某個類別的所有帖子,只需要使用這個URL:mysite.com/category/mycategory。是否有類似的方式來顯示帖子格式的所有帖子?或者任何其他方式對我來說也很好。顯示帖子格式的所有帖子。

回答

12

您可以使用tax_query parameters通過post_format獲取帖子。例如:

$args = array(
    'post_type'=> 'post', 
    'post_status' => 'publish', 
    'order' => 'DESC', 
    'tax_query' => array(
     array(
      'taxonomy' => 'post_format', 
      'field' => 'slug', 
      'terms' => array('post-format-aside') 
     ) 
    ) 
); 

然後你就可以在與get_posts()的結果迭代(或使用WP_Query()和一個標準的循環):

$asides = get_posts($args); 
if (count($asides)) { 
    foreach ($asides as $aside) { 
     // do something here... 
    } 
} 
-3

See The Loop

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

上面列出的代碼都在你的WordPress網站的帖子,所以表現出從類別中的所有職位,你需要使用if (in_category('CategoryNumberHere ')功能只從一個類別獲取職位。您還必須知道WordPress安裝中的類別數量。

看看上面的鏈接頁面,它有一個完整的教程和如何做這種事情的佈局。類別的代碼是第二部分。

+1

謝謝,但這不是我想要的。我想要獲取帖子格式的所有帖子,而不是類別。 – Jack

2
// this will get all 'quote' post format  
$args = array(
    'tax_query' => array(
     array(
      'taxonomy' => 'post_format', 
      'field' => 'slug', 
      'terms' => array('post-format-quote') 
      ) 
     ) 
    ); 
$query = new WP_query($args); 
while($query->have_posts()) : $query->the_post(); 
// do whatever you want with it 
endwhile; 
相關問題