【问题标题】:Wordpress - List all category child post when in parent categoryWordpress - 在父类别中列出所有类别子帖子
【发布时间】:2016-08-05 08:26:24
【问题描述】:

我希望能够在浏览父类别时列出所有帖子(子和父)。

像这样:

  • 家长(显示孩子 1 和孩子 2 的帖子)
    • 孩子 1(仅显示孩子 1 的帖子)
    • 孩子 2(仅显示孩子 2 的帖子)

使用下面的代码(放置在 category.php 中)当我在父类别中时,我没有得到所有的帖子。我只从一个子类别而不是几个子类别中获取帖子。

有什么办法解决这个问题吗?

<?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(); ?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我认为您的查询存在一些问题。

    1. query_posts/get_posts 函数不支持“child_of”。相反,它只是 get_categories 的有效选项。我认为您对在这里查询帖子和类别感到困惑。您可以检查 parse_queryget_posts 功能以查看所有可用选项。

    https://codex.wordpress.org/Plugin_API/Action_Reference/parse_query https://developer.wordpress.org/reference/functions/get_posts/

    1. “meta_key”选项显示,但“meta_value”缺失。

    2. 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 using get_posts
         // then merge them to the $posts_to_display array
    }
    
    //Do what ever you want with the array
    

    或者,如果您仍想使用 query_posts 和不使用 get_posts 的 Wordpress 循环,请使用可以挂钩到 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' );
    
       
       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多