Wordpress:按分类法term_id动态查询/过滤帖子

时间:2014-11-05 10:39:19

标签: wordpress taxonomy wp-query

我有一个助行器来显示分类法的术语(参见帖子here)。这是一个三层深度的分层列表:

分类

  1. 条款(包含所有子级别的帖子数量)

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

    1.1.1。 subsubterms(帖子数量)

    • 显示类别为“Subsubterms”的帖子

    1.1.2 subsubterms(帖子数量)

    • 显示类别为“Subsubterms”的帖子
  2. 条款显示正常,但不显示子文章级别的帖子。 以下代码仅显示“未分类”类别中的帖子。

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

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

    'term' => $subsubterm->term_id
    

    谢谢!

1 个答案:

答案 0 :(得分:0)

将类别替换为您的实际分类。 使用以下代码:

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

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

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