1

In a site that I am building I am trying to create a template which will display all my "product" custom post types along with their parent and child taxonomies in some hierarchical structure which is very simple:

Parent taxonomy -> Child taxonomy (if exists) -> posts

I was looking all over the web and surprisingly could not fund an answer..anywhere.

I ended up creating the following code:

<?php
// List posts by the terms for a custom taxonomy of any post type

$post_type = 'product';
$tax = 'products';
$tax_args = array(
    'order' => 'DESC'
);
    $tax_terms = get_terms( $tax, $tax_args );
    if ($tax_terms) {
        foreach ($tax_terms  as $tax_term) {
        $args = array(
            'post_type' => $post_type,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'caller_get_posts'=> 1
        );

    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) : ?>

    <h4><?php echo $tax_term->name; ?></h4>

                <ul>
                    <?php
                    $my_query->query(array( 'post_type'=>'product', 'showposts'=>'6')); 
                    ?>
                    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                        <li><a href="<?php the_permalink() ?>">
                        <?php the_title(); ?>
                        </a>
                        </li>

                    <?php endwhile; // end of loop ?>
                </ul>

        <?php else : ?>
        <?php endif; // if have_posts()
        wp_reset_query();

        } // end foreach #tax_terms
    }
    ?>

This is almost what I need. I am getting this structure:

Parent Taxonomy Term
- Post Type
- Post Type
- Post Type

Child Taxonomy Term
- Post Type
- Post Type
- Post Type

Where what I need is this:

Parent Taxonomy Term
-- Child Taxonomy Term
--- Post Type
--- Post Type
--- Post Type

Parent Taxonomy Term
-- Child Taxonomy Term
--- Post Type
--- Post Type
--- Post Type

So I understand I need to add something for the child terms but I can't figure out how to do it. thought it is simple but couldn't find any answer

would love to get a help here thanks

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.