简体   繁体   中英

limit posts pulled in from get_terms

I have the following code, which basically pulls in a list of terms for the taxonomy "categories". It then pulls in every post for that term.

 $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>";
  }
}

My question is how would i go about limiting the query to only pull in 1 post from each term?

Any help would be greatly appreciated, Cheers Dan

You can use post_count in $wpq array

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

More about WP_Query http://codex.wordpress.org/Class_Reference/WP_Query

Here is what i got working:

i simply changed the following:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM