Create a custom taxonomy template that loops through child categories in term order?

Here’s what we’ve come up with so far, and there’s definitely room for improvement.


remove_action( 'genesis_loop', 'genesis_do_loop' );

add_action( 'genesis_loop', 'ahso_custom_taxonomy_archive_loop' );

function ahso_custom_taxonomy_archive_loop() {

    global $wp_query; // globalize it.

    // get category ID
    $cat_id = $wp_query->get_queried_object_id(); // get current category

    // get all child categories
    $cat_args = array(
        'type'                     => 'products',
        'child_of'                 => $cat_id,
        //'parent'                   => '',
        'orderby'                  => 'term_group',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'product-categories',
        'pad_counts'               => false
    );

    // get all children of the current category
    $categories = get_categories( $cat_args );

    // if the cat has kittens, loop thru them in term order
    if ( !empty($categories) ) {

        foreach ($categories as $category) {

            // first output the category archive headline
            // gets the term table w/ the meta info added by genesis that contains the archive headline and intro text
            $term = get_term_by( 'id', $category->term_id, $category->taxonomy );

            echo '';
            echo ''.$term->meta["headline"].'';
            echo '';

            // then build a query to loop thru the posts in that cat

            // WP_Query arguments
            $args = array (
                'post_type'              => 'products',
                'order'                  => 'ASC',
                'orderby'                => 'menu_order',
                'tax_query'                 => array(
                    array(
                        'taxonomy'          => 'product-categories',
                        'field'             => 'id',
                        'terms'             => $category->term_id,
                        'include_children'  => false, // no kittens
                    )
                )
            );

            // loop thru posts in category
            // output featured image

            // The Query
            $cat_query = new WP_Query( $args );

            // The Loop
            if ( $cat_query->have_posts() ) {
                while ( $cat_query->have_posts() ) {
                    $cat_query->the_post();

                    // do something
                    echo '';
                    echo '';
                    echo the_post_thumbnail('medium');
                    echo '';
                    echo ''.get_the_title().'';
                    echo '';

                }
            } else {
                // no posts found
            }

            // Restore original Post Data
            wp_reset_postdata();

        }

    } // end category children loop

    // if the category has no children, just run a loop thru that category.
    // get_categories returns an empty array
    if ( empty($categories) ) {

        //echo 'this cat has no kittens';

        // WP_Query arguments
        $args = array (
            'post_type'              => 'products',
            'order'                  => 'ASC',
            'orderby'                => 'menu_order',
            'tax_query'                 => array(
                array(
                    'taxonomy'          => 'product-categories',
                    'field'             => 'id',
                    'terms'             => $cat_id, // get the initial $cat_id, from the query object
                    'include_children'  => false, // pls spay and neutr yrs pets
                )
            )
        );

        // The Query
        $cat_query = new WP_Query( $args );

        // The Loop
        if ( $cat_query->have_posts() ) {
            while ( $cat_query->have_posts() ) {
                $cat_query->the_post();

                // do something
                echo '';
                echo '';
                echo the_post_thumbnail('medium');
                echo '';
                echo ''.get_the_title().'';
                echo '';

            }
        } else {
            // no posts found
        }

        // Restore original Post Data
        wp_reset_postdata();


    } // end no children category loop

}

If you have any ideas, let us know. Feedback is always welcome.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)