2013-02-09 136 views
3

所以我有關於WordPress內循環的這個問題。
我在我的網站的結構如下:
顯示子類別和來自父類別的帖子

  • 父類別1
    • 兒童類1
    • 兒童類2
  • 父類別2
    • 兒童類1
    • Child c ategory 2
      • 郵政Cat2.1
      • 郵政Cat2.2
    • 張貼1
    • 後2

在這種案例「父類別2」具有子類別和帖子。我需要在「父類別頁面」中顯示的是子類別及其沒有子類別帖子的帖子。
我到處找,但似乎我一直在尋找錯誤的,任何幫助將是非常讚賞

回答

0

,我認爲這可以幫助:

$cat_id = get_query_var('cat'); 

if(! empty($cat_id)) { 
$category = get_category($cat_id, ARRAY_A); 

if(! empty($category)) { 
    // parent category 
    if($category['parent'] === '0') { 

     // get child IDs 
     $terms = get_term_children($cat_id, 'category'); 

     foreach($terms as $term_id) { 
      $child_category = get_term($term_id, 'category'); 

      // if you need the category 
      $category_name = $child_category->name; 


      /** 
      * Sample data in your child category object: 
      * 
      * object(stdClass)[365] 
         public 'term_id' => string '14' (length=2) 
         public 'name' => string 'Child 1' (length=7) 
         public 'slug' => string 'child-1' (length=7) 
         public 'term_group' => string '0' (length=1) 
         public 'term_taxonomy_id' => string '14' (length=2) 
         public 'taxonomy' => string 'category' (length=8) 
         public 'description' => string '' (length=0) 
         public 'parent' => string '12' (length=2) 
         public 'count' => string '5' (length=1) 
         public 'object_id' => string '33' (length=2) 
      * 
      * 
      */ 

      // do whatever you like with the categories now... 
     } 

    } else { // in child category 
     // do the regular loop here, if (have_posts()) ... 
    } 
} 
} 

也是它的發佈Here

希望可以幫助你:)