14

With WooCommerce, I want to display all the categories in a store as headings, with all of their products listed below in an unordered list. Is this possible to do? I've seen a few things that'll let me display a list of categories or a list of products for a specific category, but nothing that'll loop through everything the way I described.

Here's what I'm currently using to list all categories:

<?php
$args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
    foreach ( $product_categories as $product_category ) {
        echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</h4>';
    }
}
?> 
5
  • You simply need a loop of loops. Inside your foreach(), run a new WP_Query() to grab all the products in that term.. and then loop through those. Mar 25, 2014 at 21:09
  • I think I understand how to do this, but I can't find anything about listing products by category with PHP (all I can find is shortcode nonsense). If you can show me what that code looks like, I should be able to figure out the rest. Mar 25, 2014 at 21:23
  • 2
    You don't need a shortcode, listing products by category is just a Tax Query. Mar 25, 2014 at 21:29
  • I knew I didn't need a shortcode, I was saying that's all I could find, which was unhelpful. That link you provided looks promising, I'll give it a shot tomorrow and report back, thanks. Mar 25, 2014 at 22:04
  • 1
    Ok. If you are still stuck, edit your question with your new coding attempt and I'll take a look. Mar 25, 2014 at 23:02

1 Answer 1

26

Figured it out! The code below automatically lists all categories and each categories posts!

$args = array(
    'number'     => $number,
    'orderby'    => 'title',
    'order'      => 'ASC',
    'hide_empty' => $hide_empty,
    'include'    => $ids
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
    foreach ( $product_categories as $product_category ) {
        echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
        $args = array(
            'posts_per_page' => -1,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    // 'terms' => 'white-wines'
                    'terms' => $product_category->slug
                )
            ),
            'post_type' => 'product',
            'orderby' => 'title,'
        );
        $products = new WP_Query( $args );
        echo "<ul>";
        while ( $products->have_posts() ) {
            $products->the_post();
            ?>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </li>
            <?php
        }
        echo "</ul>";
    }
}
4
  • Nice. If you want to get really crazy you might want to look into the Transients API... that would help keep you from running so many queries on every page load. Mar 26, 2014 at 17:16
  • How can I get the image thumbnails for each category? Mar 6, 2016 at 5:57
  • @AlyssaReyes categories don't inherently have thumbnails; did you set up a custom field for your categories for this? Could you post this in a new question with more detail and send me the link so I can better understand? Mar 7, 2016 at 14:28
  • 1
    Thanks man, you saved me some time and set me in the right direction. The only way I could improve this answer is to use WooCommerce's built-in query class: WC_Product_Query, instead of WP_Query, then use a foreach loop instead of a while loop. For reasons why, take a look at the Github documentation for the query: github.com/woocommerce/woocommerce/wiki/…, but the gist is: > "custom WP_Queries queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance." Jul 5, 2019 at 13:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.