Created
September 23, 2019 03:02
-
-
Save westcoastdigital/fff6d5447255265455600dbc1b9f0259 to your computer and use it in GitHub Desktop.
WooCommerce product_cat term by product_brand custom term
This file contains hidden or 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 | |
defined('ABSPATH') || exit; | |
get_header('shop'); | |
$rubs = get_queried_object(); | |
/** | |
* Hook: woocommerce_before_main_content. | |
* | |
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content) | |
* @hooked woocommerce_breadcrumb - 20 | |
* @hooked WC_Structured_Data::generate_website_data() - 30 | |
*/ | |
do_action('woocommerce_before_main_content'); | |
?> | |
<header class="woocommerce-products-header"> | |
<h1 class="woocommerce-products-header__title page-title"><?php echo __('Rubs', 'wcd');?></h1> | |
<?php | |
/** | |
* Hook: woocommerce_archive_description. | |
* | |
* @hooked woocommerce_taxonomy_archive_description - 10 | |
* @hooked woocommerce_product_archive_description - 10 | |
*/ | |
do_action('woocommerce_archive_description'); | |
?> </header> | |
<?php | |
$terms = get_terms( 'product_brand', array( | |
'hide_empty' => 1 | |
) ); | |
if( has_term($rubs->slug, $rubs->taxonomy) ) { | |
foreach ( $terms as $term ) { | |
// Define the query | |
$args = array( | |
'orderby' => 'menu_order', | |
'hide_empty' => true, | |
'tax_query' => array( | |
'relation' => 'and', | |
array( | |
'taxonomy' => 'product_brand', | |
'terms' => $term->term_id, | |
'operator' => 'IN', | |
), | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => 'rubs', | |
'operator' => 'IN', | |
) | |
) | |
); | |
$query = new WP_Query( $args ); | |
// output the term name in a heading tag | |
if( has_term( 'rubs', 'product_cat' ) || has_term( $term->slug, 'product_brand') ) { | |
echo'<h2 class="brand-title">' . $term->name . '</h2>'; | |
} | |
// output the post titles in a list | |
echo '<ul class="rub-wrapper">'; | |
// Start the Loop | |
while ( $query->have_posts() ) : $query->the_post(); ?> | |
<?php wc_get_template_part( 'content', 'product' ); ?> | |
<?php endwhile; | |
echo '</ul>'; | |
// use reset postdata to restore orginal query | |
wp_reset_postdata(); | |
} | |
} | |
/** | |
* Hook: woocommerce_after_main_content. | |
* | |
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content) | |
*/ | |
do_action('woocommerce_after_main_content'); | |
/** | |
* Hook: woocommerce_sidebar. | |
* | |
* @hooked woocommerce_get_sidebar - 10 | |
*/ | |
do_action('woocommerce_sidebar'); | |
get_footer('shop'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment