2017-07-29 69 views
0

希望有人可以幫助我這個,我有這個代碼工作正常,它顯示在頂級類別(dieta)點擊他們的子類別,並在最後的職位名單。問題是我不能過濾輸出只顯示一個子類別(由2個子類別組成),所有這些子類別都列在togheter(子類別 - 子子類別 - 子子類別)中(其中9個子類別總!)。我怎麼能一次列出樹的一個級別?wordpress只顯示子類別的第一級

網站例如:http://www.dietaedesercizi.it/category/dieta/

<?php 
/** 
* Template Name: menu 
* 
* @package Binox - Diet Walk 
*/ 
get_header(); ?> 

    <p>pagina menu</p> 
<div id="categorie"> 
<?php if (is_category()) { 

    $this_category = get_category($cat); 

    if (get_category_children($this_category->cat_ID) != "") { 
     echo '<div id="catlist"><ul>'; 
      $childcategories = get_categories(array(
       'orderyby' => 'name', 
       'hide_empty' => false, 
       'child_of' => $this_category->cat_ID 
      )); 

     foreach($childcategories as $category) { 
      echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a>'; 
      echo '<p>'.$category->description.'</p>'; 
     } 
     echo '</ul></div>'; 
    } else { 

     if (have_posts()) : while (have_posts()) : the_post(); 
     the_content(); 
    endwhile; 
endif; 
    } 
} 
?> 

</div> 
<?php post_navigation(); ?> 

<?php get_sidebar(); ?> 

<?php get_footer(); ?> 

感謝您的幫助!

+0

如果你可以用一些演示數據創建預期的樹,我可以幫你,我之前做過這件事。 –

+0

對不起,我創建了一個更清晰的英語測試樹,它從頂級菜單父級開始稱爲「級別1」,然後它轉到 - > level2a和level2b,在這裏你可以看到問題,而不是隻有這2個直接子類別,混合在一起,3級的其他子類別(3a級和b級)! http://www.dietaedesercizi.it/category/level1/ –

回答

0

使用get_term()功能,並建立自己的菜單,或者您想 什麼,先把父類:

$categories = get_terms( 
    'category', 
    array('parent' => 0) 
); 

比foreach循環得到類別的孩子的是這樣的:

foreach ($categories as $category) : 
    $childs = get_terms( 
       'category', 
       array('parent' => $category->term_id) 
      ); 
    foreach($childs as $child){ 
     echo $child->name; 
    } 
endforeach; 
+0

謝謝你的回答!我試過代碼,但我不明白我要把第一部分放在哪裏,我正在做的是創建一個基於類別的「逐頁菜單」,我有一個wordpress上面的代碼,創建了一個更清晰的演示惠普級別1 - 2和3,並在最後的子子類別3a b和c中創建示例帖子。這裏是演示:http://www.dietaedesercizi.it/category/level1/ –

0

謝謝你你的幫助,我發現了另一個代碼,將它與我擁有的代碼混合並得到了解決方案!

那就是:

<div id="categorie"> 
<?php if (is_category()) { 

    $this_category = get_category($cat); 

    if (get_category_children($this_category->cat_ID) != "") {$cat_id = get_query_var('cat'); 
$args=array(
      'parent' => $cat_id, 
      'hide_empty' => 0, 
      'orderby' => 'name', 
      'order' => 'ASC' 
     ); 
     $categories=get_categories($args); 
     foreach($categories as $category) { 
      echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a>'; } 

     echo '</ul></div>'; 
    } else { 

     if (have_posts()) : while (have_posts()) : the_post(); 
     the_content(); 
    endwhile; 
endif; 
    } 
} 
?> 

</div> 

現在我測試它,但它似乎是罰款!謝謝!

相關問題