Skip to content

Instantly share code, notes, and snippets.

@swoboda
Created May 16, 2017 08:49
Show Gist options
  • Save swoboda/77a32886eca49fedca3c48116d26546f to your computer and use it in GitHub Desktop.
Save swoboda/77a32886eca49fedca3c48116d26546f to your computer and use it in GitHub Desktop.
categories-products-loop
<?php
add_action( 'genesis_loop', 'wpdesk_do_product_loop' );
/**
* CATEGORIES DISPLAY WITH PRODUCTS
*
*/
function wpdesk_do_product_loop() {
$product_cat_args = array(
'orderby' => 'include',
'hide_empty' => true,
'parent' => 0
);
$product_terms = get_terms( 'product_cat', $product_cat_args );
?>
<section class="category-menu entry-tags">
<?php foreach( $product_terms as $product_taxonomy ) { ?>
<a class="scroll-to" href="#category-<?php echo $product_taxonomy->slug; ?>"><?php echo $product_taxonomy->name; ?></a>
<?php } ?>
</section>
<div class="products-loop">
<div class="categories">
<?php
foreach( $product_terms as $product_taxonomy ) {
$product_term_id = $product_taxonomy->term_id;
$product_term_slug = $product_taxonomy->slug;
$product_term_name = $product_taxonomy->name;
?>
<div id="category-<?php echo $product_term_slug; ?>" class="category">
<h2>
<a href="<?php echo get_term_link( $product_term_slug, 'product_cat' ) ?>"><?php echo $product_term_name; ?></a>
</h2>
<ul class="products">
<?php
$product_tax_post_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_term_id
)
)
);
$product_tax_post_qry = new WP_Query($product_tax_post_args);
if ( $product_tax_post_qry->have_posts() ) :
while ( $product_tax_post_qry->have_posts() ) :
$product_tax_post_qry->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
else :
echo "No posts";
endif;
?>
</ul>
</div>
<?php
}
?>
</div>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment