• I am looking to list the posts on my archive page as follows:

    Main Category
    – Sub Category
    — Sub Category Products
    – Sub Category
    — Sub Category Products
    – Remaining Main Category Products

    I am using a custom taxonomy and post type. As of right now I have this:

    $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;
                            }
                        }
                    }
                }

    But I’m not sure if this is correct and I’m also not sure how to grab the remaining products that are part of the main category.

  • The topic ‘List posts under category and subcategories’ is closed to new replies.