2012-09-19 46 views
0

我有以下代碼,它基本上爲分類「類別」提供了一個條目列表。然後它會抽出每個職位的任期。限制帖子從get_terms中提取

$terms = get_terms('categories'); 

foreach ($terms as $term) { 
    $wpq = array ('taxonomy'=>'categories','term'=>$term->slug); 
    $myquery = new WP_Query ($wpq); 
    $article_count = $myquery->post_count; 
    echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">"; 
    echo $term->name; 
    echo "</h3>"; 
    if ($article_count) { 
    echo "<ul>"; 
    while ($myquery->have_posts()) : $myquery->the_post(); 
     echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>"; 
    endwhile; 
    echo "</ul>"; 
    } 
} 

我的問題是我將如何去限制查詢只從每個術語拉1職位?

任何幫助將不勝感激,乾杯丹

回答

0

這裏是我得到了什麼工作:

我只是改變了t他以下幾點:

$wpq = array ('taxonomy'=>'categories','term'=>$term->slug,); 

到:

$wpq = array ('taxonomy'=>'categories', 'showposts' => 1, 'term'=>$term->slug,); 
2

可以在$wpq陣列使用post_count

如: - $wpq = array ('taxonomy'=>'categories','term'=>$term->slug,'post_count' => 1);

更多WP_Query http://codex.wordpress.org/Class_Reference/WP_Query

+0

感謝,POST_COUNT didnt相當的工作,所以我用 「showposts」,而不是 – danyo

+2

考慮更改 'showposts' 到 'posts_per_page',爲 'showposts' 被棄用。 – maiorano84