2014-11-05 72 views
0

我有一个沃克显示分类术语(见文章here)。这是一个分层列表为3级深:(含岗位数量的所有子级别)WordPress的:按分类学动态查询/过滤帖子term_id

分类

  1. 方面

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

    1.1.1。 subsubterms(帖子的数量)

    • 显示交与类别 “Subsubterms”

    1.1.2 subsubterms(帖子的数量)

    • 显示交与类别 “Subsubterms”

这些条款显示正常,但在帖子上的帖子ubterm级别不显示。 以下代码仅显示来自“未分类”类别的帖子。

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 

谢谢!

+0

请提供完整的代码,以便可以检查 – 2014-11-05 10:45:43

+0

完成代码添加,谢谢! – george 2014-11-05 11:01:39

回答

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

这样做了!我想是时候继续前进并做其他事情了;-)我实现了你的代码片断,但它不起作用。然后我用你的完整代码和 - Tadaa - 这里是:每个subsubterm的帖子。必须在某处出现错字。无论如何,我不能够感谢你! – george 2014-11-05 11:26:32

+0

不幸的是,计数是关闭的。这个想法是计算每个级别的职位,包括其子级别。 sububterms中的帖子数量是正确的,但在子级别和term级别上,来自_all_项的帖子将被计数。 – george 2014-11-05 11:46:25

+0

你想得到什么?计数或职位列表? – 2014-11-05 11:54:57

相关问题