2013-02-09 134 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

希望可以帮助你:)