Skip to content

Instantly share code, notes, and snippets.

@stephanieland352
Created October 2, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephanieland352/0ae2fa22e8dc8d426dd60f73a2064276 to your computer and use it in GitHub Desktop.
Save stephanieland352/0ae2fa22e8dc8d426dd60f73a2064276 to your computer and use it in GitHub Desktop.
Woocommerce Categories loop with products
$args = array(
'type' => 'product',
'parent' => 0,
'orderby' => 'name',
'hide_empty' => false,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false
);
$cats = get_categories( $args );
//var_dump($cat);
foreach( $cats as $cat ){
$category_id = $cat->term_id;
$category_name = $cat->name; //category name
$category_slug = $cat->slug; //category slug
$string = $cat->name; ?>
<div class="shop-categories <?php echo $category_slug ?>">
<a href="<?php echo get_term_link($cat->slug, 'product_cat'); ?>" title="<?php echo $category_name; ?>">
<?php
$thumbnail_id = get_term_meta( $category_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src($thumbnail_id, 'large');
if($image){ ?>
<div class="featured-category-image">
<?php echo '<img src="' . $image[0] . '" alt="'.$category_name.'" />'; ?>
</div>
<?php } ?>
<h2 class="cat-title">
<?php echo $category_name; ?>
</h2>
</a>
</div>
<?php
$args = array(
'post_type' => 'product',
'product_cat' => $category_slug,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) { ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
endwhile; ?>
<?php woocommerce_product_loop_end(); ?>
<?php }
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment