Wordpress:分类术语的分层列表

时间:2014-11-04 09:47:05

标签: wordpress list taxonomy term

我在这里碰壁,虽然听起来很简单:我想返回自定义帖子类型分类术语的分层列表。我得到的是第一级术语和嵌套的uls。但子条款没有显示。有什么想法吗?

以下是代码:

function return_terms_index() {
  $taxonomies = array( 
    'taxonomy_name',
  );

  $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 ) {

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

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

        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          //return sub terms (not working :( )
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

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

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

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

  $return .= '</ul>';

return $return;
}

谢谢!

编辑:这是输出。

<ul>
  <li id="category-176">
    1. <span class="post-count">0</span><span class="cat-description" style="display: none;">Description</span>
    <ul id="subTerm-176" style="display: block;"></ul>
  </li>
  <li id="category-49">
    2. <span class="post-count">0</span><span class="cat-description" style="display: none;">Langtitel/Beschreibung</span>
    <ul id="subTerm-49" style="display: none;"></ul>
  </li>
</ul>

编辑:分类法现在在分层列表中返回,YAY! 但是我想要查询和显示三级分类术语的帖子,这些代码也不行。

$post_query = new WP_Query($taxonomies, array( 
  'term' => $subsubterm->term_id 
  )); ?>

  <?php 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;

它必须是动态的,所以我不能通过名称/ slug指定术语。但这有可能吗?

'term' => $subsubterm->term_id

再次感谢。

1 个答案:

答案 0 :(得分:2)

您错过了

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

尝试以下代码

function return_terms_index() {
  $taxonomies = array( 
    'taxonomy_name',
  );

  $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 ) {

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

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

        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          //return sub terms (not working :( )
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

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

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

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

  $return .= '</ul>';

return $return;
}