2017-07-29 2 views
0

誰かが私にこれを助けてくれることを願っています。このコードはうまくいきます。トップカテゴリー(dieta)のサブカテゴリーをクリックして、最後に投稿を表示しますリスト。 (2つのサブカテゴリで構成された)サブカテゴリの1つのレベルのみを表示するように出力をフィルタリングすることはできません。そのすべてがtogheter(サブカテゴリ - サブサブ - サブサブサブカテゴリ)にリストされます合計!)。一度にツリーのレベルを1つだけリストする方法はありますか?例の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

申し訳ありませんテスト・ツリーを英語でもっと明瞭に作成しました。トップレベルの親「level1」から始まり、「level2a」と「level2b」に移動します。ここには2つの直接サブカテゴリの代わりに問題があります。 、混合されたtogheter、レベル3(レベル3a bとc)の他の下位サブカテゴリー! http://www.dietaedesercizi.it/category/level1/ –

答えて

0

使用get_term()機能と最初の親カテゴリを取得するには、独自のメニューやあなたが欲しいもの を構築する:このようなカテゴリのチャイルズますforeachループよりも

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

を:

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

お返事ありがとうございます!私はコードを試みたが、私は最初の部分を置く必要がある、私がやっていることはカテゴリに基づいて "ページごとのページメニュー"を作成することです理解していない、私は上記のコード、最終的なサブ・サブカテゴリ3aと3bに、より明確なデモ・ウイット・レベル1〜2と3とポスト例を作成しました。ここにデモがあります: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> 

は、今私はそれをテストしていますが、問題ないように見えます!ありがとうございました!

関連する問題