2017-07-29 70 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> 

现在我测试它,但它似乎是罚款!谢谢!

相关问题