存档CPT中的所有类别以及所有CPT类别的嵌套帖子

时间:2018-10-18 19:55:57

标签: php wordpress custom-post-type

因此,我正在WordPress中建立自己的知识库。大多数插件对于我的需求来说太胖了。

基本上,我已经设置了“自定义帖子类型”,并创建了类别和帖子,但是我无法在相对类别下嵌套帖子。

我希望存档页面显示CPT的所有类别,然后嵌套相对于它们的帖子。我已经迈出了第一步,只是没有迈出最后一步,而且我不明白自己在做什么错。

代码:

<?php
$taxonomy = 'category';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
    <?php foreach ( $terms as $term ) { ?>
        <pre><?php print_r($term); ?></pre>
        <div class="article">
            <div class="article__box">
                <div class="article__header"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></div>
                <div class="article__content">
                <?php
                $posts_array = get_posts(
                    array( 'showposts' => -1,
                    'post_type' => 'knowledgebase',
                    'tax_query' => array(
                            array(
                                'taxonomy' => 'category',
                                'fields' => $term->id,
                                'terms' => $term->name,
                            )
                        )
                    )
                );
                print_r( $posts_array ); 
                ?>
                </div>
            </div>
        </div>
    <?php } ?>
<?php endif;?>
</div>

所需效果:http://prntscr.com/l7opcu

当前拥有:http://prntscr.com/l7opqu

0 个答案:

没有答案