2016-08-05 143 views
0

我想能夠列出瀏覽父類別時的所有帖子(子女和父母)。WordPress的 - 在父類別中列出所有類別的子帖子

像這樣:

  • 父(從兒童1兒童2顯示帖子)
    • 兒童1(顯示自兒童1只職位)
    • 兒童2(顯示自獨生子女帖子2)

有了下面這段代碼(放在category.php)我不明白人l當我在父類別中時的帖子。我只是從一個孩子類別而不是幾個類別發佈帖子。

任何想法如何解決這個問題?

<?php get_header(); ?> 
    <div class="container"> 
     <div class="row"> 
      <?php get_template_part('include-cat-tag'); ?> 
      <div class="col-xs-12 col-sm-9 col-md-9 list-page-middle"> 
       <header class="clearfix"> 
        <h1><?php single_cat_title('', true); ?></h1> 
       </header> 
       <?php 
        wp_reset_query(); 
        $categories = get_the_category(); 
        $category_id = $categories[0]->cat_ID; 
        $args = array(
         'posts_per_page' => 100, 
         'category__in' => array($category_id), 
         'orderby' => 'meta_value title', 
         'order' => 'ASC', 
         'post_status' => 'publish', 
         'meta_key' => 'betyg', 
         'child_of' => $category_id 
        ); 
        query_posts($args); 
        if (have_posts()): while (have_posts()) : the_post(); 
        get_template_part('include-list-post'); 
       ?> 
       <?php endwhile; ?> 
       <?php else: ?> 
        <?php get_template_part('include-no-post'); ?> 
       <?php endif; ?> 
      </div>  
     </div> 
     <?php 
      get_template_part('include-list'); 
      get_template_part('include-social'); 
     ?> 
     </div> 
    </div> 
    <?php get_footer(); ?> 

回答

0

我認爲您的查詢存在一些問題。

  1. 「child_of」 不支持query_posts/get_posts功能。相反,它只是get_categories的有效選項。我認爲你在查詢帖子和類別之間感到困惑。您可以查看parse_queryget_posts函數查看所有可用選項。

https://codex.wordpress.org/Plugin_API/Action_Reference/parse_query

https://codex.wordpress.org/Template_Tags/get_posts

  • 「meta_key」 選項示出,但 「meta_value」 缺失。

  • WP也不建議直接使用query_posts,而是使用get_posts函數或掛鉤到pre_get_posts操作。

  • 要獲得的類別的子類別中的所有文章列表,你可以做一些事情如下:

    <?php ... 
        $categories = get_categories(array("child_of" => $parent_category->term_id)); 
    
        $posts_to_display = get_posts("category" => $parent_category->term_id); 
        foreach($categories as $category){ 
    
        // query child posts of this category here of this category here using get_posts 
    
        // then merge them to the $posts_to_display array 
        } 
    
        //Do what ever you want with the array 
    

    或者,如果你仍然想利用query_posts和WordPress的循環不使用get_posts,使用能勾到pre_get_posts行動:

    <?php 
    function get_posts_for_parent_category($query) { 
        // make sure that you are in the category page not single post or custom post type page 
        if(!$query->is_category) 
         return; 
    
        $parent_category = $query->get_querried_object(); 
        $categories_to_query = array($parent_category->term_id); 
    
        // Using get_categories to get all child categories_to_query 
    
        // Get their ids and add it to the $categories_to_query array 
    
        $query->set("category__in",$categories_to_query); 
    } 
    add_action('pre_get_posts', 'get_posts_for_parent_category');