2016-03-23 146 views
1

此代碼提取了一個類別列表,但我只需要顯示正在查看的當前類別的子類別。有任何想法嗎?WordPress的:吐出兒童類別列表?

<ul class="categoryNav"> 
     <?php 
      $args = array(
      'show_option_all' => '', 
      'orderby'   => 'name', 
      'order'    => 'ASC', 
      'style'    => 'list', 
      'show_count'   => 0, 
      'hide_empty'   => 1, 
      'child_of'   => 0, 
      'feed'    => '', 
      'feed_type'   => '', 
      'exclude'   => '', 
      'exclude_tree'  => '', 
      'include'   => '', 
      'hierarchical'  => 1, 
      'title_li'   => __(''), 
      'show_option_none' => __(''), 
      'number'    => null, 
      'echo'    => 1, 
      'depth'    => 1, 
      'current_category' => 0, 
      'pad_counts'   => 0, 
      'taxonomy'   => 'product_cat', 
      'walker'    => null 
      ); 
      wp_list_categories($args); 
     ?> 
    </ul> 

回答

1

我發現下面似乎工作:

$cat = get_queried_object(); 
$cat_id = $cat->term_id; 
$args = array(
'style' => 'list', 
    'hide_empty' => 1, 
    'child_of' => $cat_id, 
    'hierarchical' => 1, 
    'depth' => 1, 
    'taxonomy' => 'product_cat' 
); 
wp_list_categories($args); 

我希望別人有所幫助:此方法似乎並不被據我所知其他任何地方記錄。它非常適合分類導航。

0

你必須設置爲0child_of查詢參數。將其設置爲當前查看的類別。像:

$cat_id = get_query_var('cat'); 
$args = array(
    ... 
    'child_of'   => $cat_id, 
    ... 
); 
wp_list_categories($args); 
+0

我試過了,我創建的所有類別似乎都在子類別 –

+0

上顯示,看起來$ cat_id仍然是0.嘗試將它手動設置爲所需的ID,然後編寫一個函數來設置它動態地取決於你在哪裏 – niklas