Wordpress:按分类法 term_id 动态查询/过滤帖子 [英] Wordpress: Dynamically query/filter posts by taxonomy term_id

查看:20
本文介绍了Wordpress:按分类法 term_id 动态查询/过滤帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个助行器来显示分类法的术语(参见此处).这是一个有 3 层深的分层列表:

I have a walker to display terms of a taxonomy (see post here). It's a hierarchical list that is 3 levels deep:

分类

  1. 条款(帖子数量,包括所有子级别)

  1. terms (number of posts incl. all sub levels)

1.1.subterms(帖子数量,包括 subsubterms 1 & 2)

1.1. subterms (number of posts incl. subsubterms 1 & 2)

1.1.1.subsubterms(帖子数)

1.1.1. subsubterms (number of posts)

  • 显示类别为Subsubterms"的帖子

1.1.2 subsubterms(帖子数)

1.1.2 subsubterms (number of posts)

  • 显示类别为Subsubterms"的帖子

术语显示正常,但子子级级别的帖子不显示.以下代码仅显示来自未分类"类别的帖子.

The terms display fine, but the posts on subsubterm-level do not display. The following code only displays post from the "Uncategorized" category.

add_filter( 'XYZ_index', 'return_XYZ_index' );

function return_XYZ_index()
{

  $taxonomies = array( 
    'XYZ'
  );

  $args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false,
    'fields'            => 'all',
    'parent'            => 0,
    'hierarchical'      => true,
    'child_of'          => 0,
    'pad_counts'        => false,
    'cache_domain'      => 'core'
  );

  $terms = get_terms($taxonomies, $args);    

  $return .= '<ul>';     

  foreach ( $terms as $term ) {

        $subterms = get_terms($taxonomies, array(
          'parent'   => $term->term_id,
          'hide_empty' => false
          ));

        //count posts in terms plus subterms
          $count = 0;
          $countargs = array(
            'child_of' => $term->term_ID,
            );
          $tax_terms = get_terms($taxonomies,$countargs);
          foreach ($tax_terms as $tax_term) {
            $count +=$tax_term->count;
            }

      // return terms 
      $return .= sprintf(
        '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $count . '</span><span class="cat-description">%3$s</span>',       
        $term->term_id,
        $term->name,
        $term->description
      );

      //count posts in subterm
      $countsub = 0;
      $countsubargs = array(
        'child_of' => $subterm->term_id,
        );
      $tax_term_subs = get_terms($taxonomies, $countsubargs);
      foreach ($tax_term_subs as $tax_term_sub) {
        $countsub +=$tax_term_sub->count;
      }


        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          $subsubterms = get_terms($taxonomies, array(
            'parent'   => $subterm->term_id,
            'hide_empty' => false
            ));            

          // return subterms
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $countsub . '</span><span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

          $return .= '<ul>';

          foreach ( $subsubterms as $subsubterm ) {

            // return subsubterms
            $return .= sprintf(
            '<li id="category-%1$s" class="toggle">%2$s <span class="post-count">%3$s</span><span class="cat-description">%4$s</span>',       
            $subsubterm->term_id,
            $subsubterm->name,
            $subsubterm->count,
            $subsubterm->description
            );

            // return posts (**EDITED CODE**)

            $postargs = array(
            'tax_query' => array(
              array(
                'taxonomy' =>'XYZ,
                'field' => 'id',
                'terms' =>  $subsubterm->term_id
                )
              )
            );
            $post_query = new WP_Query( $args ); 
             if ( $post_query->have_posts() ) : 
                $return .= '<ul>';
                 while ( $post_query->have_posts() ) : $post_query->the_post();
                    $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
                endwhile;
                $return .= '</ul>';

             wp_reset_postdata();
             else:
             endif;

            $return .= '</li>'; //end subsubterms li
          }

          $return .= '</ul>'; //end subsubterms ul

        $return .= '</li>'; //end subterms li 
        }            

        $return .= '</ul>'; //end subterms ul

      $return .= '</li>'; //end terms li
    } //end foreach term        

    $return .= '</ul>';

  return $return;
}

我不确定这部分是否可行:

I'm not sure if this part is even possible:

'term' => $subsubterm->term_id

谢谢!

推荐答案

用您的实际分类法替换类别.使用以下代码:

Replace category with your actual taxonomy. Use following code:

          $args = array(
              'tax_query' => array(
              array(
              'taxonomy' =>'category',
              'field' => 'id',
              'terms' =>  $subsubterm->term_id
              )
            )
      );
$post_query = new WP_Query( $args ); 
 if ( $post_query->have_posts() ) : 
    $return .= '<ul>';
     while ( $post_query->have_posts() ) : $post_query->the_post();
        $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
    endwhile;
    $return .= '</ul>';

 wp_reset_postdata();
else:
 endif;

或者您可以使用下面提供的完整代码:

Or you can use complete code provided below:

function return_XYZ_index()
{

  $taxonomies = array( 
    'XYZ'
  );

  $args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false,
    'fields'            => 'all',
    'parent'            => 0,
    'hierarchical'      => true,
    'child_of'          => 0,
    'pad_counts'        => false,
    'cache_domain'      => 'core'
  );

  $terms = get_terms($taxonomies, $args);

  $return .= '<ul>'; 

  foreach ( $terms as $term ) {

        $subterms = get_terms($taxonomies, array(
          'parent'   => $term->term_id,
          'hide_empty' => false
          ));

        //count posts in terms plus subterms
          $count = 0;
          $countargs = array(
            'child_of' => $term->term_ID,
            );
          $tax_terms = get_terms($taxonomies,$countargs);
          foreach ($tax_terms as $tax_term) {
            $count +=$tax_term->count;
            }

      // return terms 
      $return .= sprintf(
        '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $count . '</span><span class="cat-description">%3$s</span>',       
        $term->term_id,
        $term->name,
        $term->description
      );

      //count posts in subterm
      $countsub = 0;
      $countsubargs = array(
        'child_of' => $subterm->term_id,
        );
      $tax_term_subs = get_terms($taxonomies, $countsubargs);
      foreach ($tax_term_subs as $tax_term_sub) {
        $countsub +=$tax_term_sub->count;
      }


        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          $subsubterms = get_terms($taxonomies, array(
            'parent'   => $subterm->term_id,
            'hide_empty' => false
            ));            

          // return subterms
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $countsub . '</span><span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

          $return .= '<ul>';

          foreach ( $subsubterms as $subsubterm ) {

            // return subsubterms
            $return .= sprintf(
            '<li id="category-%1$s" class="toggle">%2$s <span class="post-count">%3$s</span><span class="cat-description">%4$s</span>',       
            $subsubterm->term_id,
            $subsubterm->name,
            $subsubterm->count,
            $subsubterm->description
            );

            // return posts
             $args = array(
                  'tax_query' => array(
                  array(
                  'taxonomy' =>'category',
                  'field' => 'id',
                  'terms' =>  $subsubterm->term_id
                  )
                )
          );
    $post_query = new WP_Query( $args );  if ( $post_query->have_posts() ) : 
              $return .= '<ul>';
              while ( $post_query->have_posts() ) : $post_query->the_post(); 
                $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
              endwhile;
              $return .= '</ul>';

              wp_reset_postdata();

            else:
            endif;

            $return .= '</li>'; //end subsubterms li
          }

          $return .= '</ul>'; //end subsubterms ul

        $return .= '</li>'; //end subterms li 
        }            

        $return .= '</ul>'; //end subterms ul

      $return .= '</li>'; //end terms li
    } //end foreach term        

    $return .= '</ul>';

  return $return;
}

这篇关于Wordpress:按分类法 term_id 动态查询/过滤帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆