Wordpress查询每个父类别的一个帖子(不包括子类别)

时间:2011-05-06 22:45:48

标签: wordpress categories

我正在尝试列出Wordpress中每个父类别的一个帖子,但我似乎无法弄清楚如何更改此代码以排除子类别。基本上我列出了类别的名称,然后列出了每个父类别的post thumb,excerpt和read more按钮。我不能只按类别ID排除子类别,因为此网站适用于稍后将添加子类别的客户端。请帮忙,谢谢!

$num_cats_to_show = 10;
$count = 0;
$taxonomy = 'category';//  e.g. post_tag, category
$param_type = 'category__in'; //  e.g. tag__in, category__in
$term_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
);
$terms = get_terms($taxonomy, array( 'exclude' => '15,1'),$term_args );
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1,
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      $count ++;
      if ($count <= $num_cats_to_show) {
        while ($my_query->have_posts()) : $my_query->the_post(); 

1 个答案:

答案 0 :(得分:1)

我认为你只想得到父母等于0(没有父母)的那些词。

$term_args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);
$terms = get_terms($taxonomy, $term_args);

我认为您不能使用get_terms按ID排除字词,但您可以拒绝循环中的相关字词。

相关问题