【问题标题】:Wordpress query one post from each parent category (exclude child categories)Wordpress 从每个父类别中查询一篇文章(不包括子类别)
【发布时间】:2011-08-20 12:59:07
【问题描述】:

我正在尝试从 Wordpress 中的每个父类别中列出一篇文章,但我似乎无法弄清楚如何更改此代码以排除子类别。基本上我列出了类别的名称,然后是每个父类别的帖子拇指、摘录和阅读更多按钮。我不能只按类别 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(); 

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    我认为您只想获得父项等于 0(无父项)的那些术语。

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

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

    【讨论】:

    • 哦,你是对的。在问这个问题之前,我实际上尝试了您的代码,但由于我的排除条款代码(顺便说一句,它有效,但禁用了 parent=0 代码),它不起作用......那么我该如何拒绝该类别。问题是,它是“特色”类别,我仍然想要列出的类别中的项目,我只是不希望列出类别本身。就像我之前说的,我列出了每个父类别中的一个帖子,但我需要从这个列表中排除“精选”类别。
    • 想通了,感谢您的超级帮助!!!如果有人遇到这种情况,这可行:$term_args=array( 'orderby' =&gt; 'name', 'order' =&gt; 'ASC', 'parent' =&gt; 0, 'exclude' =&gt; '15', ); $terms = get_terms($taxonomy,$term_args );
    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多