2014-11-04 118 views
1

雖然這聽起來很簡單,但我想要返回一個自定義帖子類型分類術語的層次列表。我得到的是術語和嵌套uls的第一級。但子條款並未顯示。有任何想法嗎?WordPresspress:分類術語的分層列表

下面的代碼:

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> 

編輯:分類法在分級列表中返回了,耶! 但我想查詢和顯示第三級分類術語的帖子,這一點的代碼沒有辦法。

$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 

再次感謝。

回答

2

你已經錯過了在

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

感謝指出了這一點!我實現了你的代碼/缺少$分類法,但輸出保持不變(請參閱編輯的文章)。 – george 2014-11-04 10:09:05

+0

代碼工作正常。檢查您的管理面板。你有沒有分配職位的任何分項? – 2014-11-04 10:12:29

+0

現在工作還是有錯誤? – 2014-11-04 10:31:39