【问题标题】:Wordpress Template - Display all posts under a parent category separated out by subcategoryWordpress 模板 - 显示由子类别分隔的父类别下的所有帖子
【发布时间】:2016-11-16 23:48:51
【问题描述】:

我想创建一个类别模板,显示父类别标题、子类别标题以及每个子类别下的所有帖子。

所以它看起来像这样

父母 子类别 发布 #1 发布 #2 子类别 发布#3 发布#4

这是我目前所拥有的,但我不知道如何前进。

   <?php if ( have_posts() ) : ?>

            <div class="section-header">
                <h1 class="page-title"><?php single_cat_title(''); ?></h1>
                <!-- <?php
                    the_archive_title( '<h1 class="page-title">', '</h1>' );
                    the_archive_description( '<div class="taxonomy-description">', '</div>' );
                ?> -->
            </header><!-- .page-header -->
            <p>Some text</p>

                <?php

                    // The Query
                    $the_query = new WP_Query( array( 'cat' => 72 ) );

                    // The Loop
                    if ( $the_query->have_posts() ) {
                        echo '<ul>';
                        while ( $the_query->have_posts() ) {
                            $the_query->the_post();
                            echo '<li>' . get_the_title() . '</li>';
                        }
                        echo '</ul>';
                        /* Restore original Post Data */
                        wp_reset_postdata();
                    } else {
                    // no posts found
                    }

                    ?>


        <?php else : ?>

            <?php ?>

        <?php endif; ?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在你的主题functions.php中

    function display_category_posts($cat_id) {
        echo get_cat_name($cat_id);
        $the_query = new WP_Query( array( 'cat' => $cat_id ) );
        // you loop code
        wp_reset_query(); //or wp_reset_postdata(), not put it in have_posts() block, we should always reset query even there no posts found.
    }
    

    在您的类别模板中

    // get current category of viewing page.
    $current_cat_obj = get_queried_object();
    
    // try to get sub categories.
    $sub_cat_ids = get_terms( 'category', array(
        'parent'    => $current_cat_obj->term_id,
        'hide_empty' => false,
        'fields' => 'ids'
    ) );
    
    // if the category of current page is a top-level category and has sub categories, display is as sub category posts view.
    if(!$current_cat_obj->parent && !empty($sub_cat_ids)) {
        foreach($sub_cat_ids as $sub_cat_id) {
            display_category_posts($sub_cat_id);
        }
    } else {
       // otherwise, display it as normal category view.
    }
    

    【讨论】:

    • 谢谢,但这给了我以下错误“警告:isset 中的非法偏移类型或 /homepages/9/d389995387/htdocs/marketsquaresj/wp-includes/class-wp-term-query 中的空白.php on line 376" 你能告诉我应该在我提供的代码中插入类别模板代码的位置吗?或者确切地说需要替换什么?
    • 对不起,我的错误。应该是object_id,不是object,我已经更新了代码,再试一次。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多