Last active
February 3, 2022 02:26
-
-
Save growdev/7930554 to your computer and use it in GitHub Desktop.
Display Category title then products in page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_shortcode( 'growdev_category_list', 'growdev_category_list'); | |
function growdev_category_list( $atts ) { | |
global $woocommerce_loop; | |
extract( shortcode_atts( array ( | |
'number' => null, | |
'orderby' => 'term_group', | |
'order' => 'ASC', | |
'columns' => '4', | |
'hide_empty' => 1, | |
'parent' => '0' | |
), $atts ) ); | |
if ( isset( $atts[ 'ids' ] ) ) { | |
$ids = explode( ',', $atts[ 'ids' ] ); | |
$ids = array_map( 'trim', $ids ); | |
} else { | |
$ids = array(); | |
} | |
$hide_empty = ( $hide_empty == true || $hide_empty == 1 ) ? 1 : 0; | |
// get terms and workaround WP bug with parents/pad counts | |
$args = array( | |
'orderby' => $orderby, | |
'order' => $order, | |
'hide_empty' => $hide_empty, | |
'include' => $ids, | |
'pad_counts' => true, | |
'child_of' => $parent | |
); | |
$product_categories = get_terms( 'product_cat', $args ); | |
if ( $parent !== "" ) | |
$product_categories = wp_list_filter( $product_categories, array( 'parent' => $parent ) ); | |
if ( $number ) | |
$product_categories = array_slice( $product_categories, 0, $number ); | |
$woocommerce_loop['columns'] = $columns; | |
ob_start(); | |
// Reset loop/columns globals when starting a new loop | |
$woocommerce_loop['loop'] = $woocommerce_loop['column'] = ''; | |
if ( $product_categories ) { | |
// woocommerce_product_loop_start(); | |
foreach ( $product_categories as $category ) { | |
echo '<h3 style="background: grey; padding: 3px 10px; color: white;">' . $category->name . " (" . $category->count . ")</h3>"; | |
growdev_category_listing( array( 'category' => $category->slug ) ); | |
//woocommerce_get_template( 'content-product_cat.php', array( | |
// 'category' => $category | |
//) ); | |
} | |
// woocommerce_product_loop_end(); | |
} | |
woocommerce_reset_loop(); | |
return '<div class="woocommerce">' . ob_get_clean() . '</div>'; | |
} | |
function growdev_category_listing( $atts ){ | |
global $woocommerce, $woocommerce_loop; | |
if ( empty( $atts ) ) return; | |
extract( shortcode_atts( array( | |
'per_page' => '12', | |
'columns' => '4', | |
'orderby' => 'title', | |
'order' => 'desc', | |
'category' => '' | |
), $atts ) ); | |
if ( ! $category ) return; | |
// Default ordering args | |
$ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderby, $order ); | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => 1, | |
'orderby' => $ordering_args['orderby'], | |
'order' => $ordering_args['order'], | |
'posts_per_page' => $per_page, | |
'meta_query' => array( | |
array( | |
'key' => '_visibility', | |
'value' => array('catalog', 'visible'), | |
'compare' => 'IN' | |
) | |
), | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'product_cat', | |
'terms' => array( esc_attr($category) ), | |
'field' => 'slug', | |
'operator' => 'IN' | |
) | |
) | |
); | |
if ( isset( $ordering_args['meta_key'] ) ) { | |
$args['meta_key'] = $ordering_args['meta_key']; | |
} | |
ob_start(); | |
$products = new WP_Query( $args ); | |
$woocommerce_loop['columns'] = $columns; | |
if ( $products->have_posts() ) : ?> | |
<?php woocommerce_product_loop_start(); ?> | |
<?php while ( $products->have_posts() ) : $products->the_post(); ?> | |
<?php woocommerce_get_template_part( 'content', 'product' ); ?> | |
<?php endwhile; // end of the loop. ?> | |
<?php woocommerce_product_loop_end(); ?> | |
<?php else : ?> | |
<p>Wah wah wah.</p> | |
<?php endif; | |
ob_flush(); | |
wp_reset_postdata(); | |
return '<div class="woocommerce">' . ob_get_clean() . '</div>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment