列出类别和子类别下的帖子(WordPress)

时间:2015-06-29 20:19:18

标签: php wordpress archive custom-taxonomy

我希望在我的存档页面上列出帖子,如下所示:

主要类别
- 子类别
- 子类别产品
- 子类别
- 子类别产品
- 剩余的主要类别产品

我知道这不是整理档案页面的最佳方式,但它是客户想要的。

我正在使用自定义分类和帖子类型。截至目前,我有这个:

$term_children = get_terms('product_category', array( 'parent' => get_queried_object_id(), ) );

        if (! is_wp_error($terms)){
            foreach ($term_children as $child){
                echo '<h3>' . $child->name . '</h3>';
                $term_grandchildren = get_terms('product_category', array('parent' => $child->term_id,));
                if(! is_wp_error($terms)){
                    foreach ($term_grandchildren as $gchild){
                        echo '<h4>' . $gchild->name . '</h4>';
                        $query_args['tax_query'] = array(
                       array(
                           'taxonomy' => $gchild->taxonomy,
                           'terms'    => (int)$gchild->term_id,
                           'field'    => $gchild->slug,
                           ),
                        );
                        $products = new WP_Query( $query_args );
                        while ( $products->have_posts() ) : $products->the_post();
                            ?>
                            <a href="<?php the_permalink();?>"><?php the_title();?></a><?php
                        endwhile;
                    }
                }
            }
        }

但我不确定这是否正确,而且我也不确定如何抓住属于主要类别的剩余产品。

1 个答案:

答案 0 :(得分:0)

选中此链接,显示来自第3级子类别的帖子

http://www.codersmount.com/2015/02/displaying-posts-from-3rd-level-sub-categories-in-wordpress/