admin管理员组文章数量:1122832
I have this code which has taken over 7 hours to put together through research but the last hours or so I have hit a brick wall!
I require the taxonomy title to be hidden if there are no posts associated with it.
Here is the page: /
Here is my code:
/**********************************************/
// CUSTOM WINE MENU SHORTCODE
/**********************************************/
add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );
function wine_list_per_cat($atts){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cat' => '', // list of categories in the format 0,1,2,3,4
'tax' => 'product_cat', // taxonomy use
'per_cat' => '3', // max featured items to display per category
'columns' => '3', // columns size
), $atts));
?>
<ul id="wine-menu">
<?php
$wine_type_terms = get_terms( 'wine-type' );
foreach ( $wine_type_terms as $wine_type_term ) {
$args = array(
'post_type' => 'product',
'showposts' => -1,
'tax_query' => array(
'relation' => 'IN',
array(
'taxonomy' => 'wine-type',
'field' => 'slug',
'terms' => array( $wine_type_term->slug ),
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat
)
));
$wine_type_query = new WP_Query($args);?>
<li>
<h2><?php echo $wine_type_term->name; ?></h2>
<ul>
<?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
<li class="wine-item">
<h3><?php the_title(); ?></h3>
<span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
<span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
<span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
<span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
<div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
<?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
</li>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
</ul>
</li>
<?php } ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
<?php }
UPDATE! OK!!! Woooohooo! Found out what it was.. I should have been calling if have posts before the call for $tax->name; Here is my final, and working code! :)
/**********************************************/
// CUSTOM WINE MENU SHORTCODE
/**********************************************/
add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );
function wine_list_per_cat($atts){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cat' => '', // list of categories in the format 0,1,2,3,4
'tax' => 'product_cat', // taxonomy use
'per_cat' => '3', // max featured items to display per category
'columns' => '3', // columns size
), $atts));
?>
<ul id="wine-menu">
<?php
$taxonomy = get_terms( 'wine-type', array('hide_empty' => true, 'pad_counts' => true));
foreach ( $taxonomy as $tax ) {
$args = array(
'post_type' => 'product',
'orderby' => 'title',
'showposts' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat,
'operator' => 'IN'
),
array(
'taxonomy' => 'wine-type',
'field' => 'slug',
'terms' => array( $tax->slug ),
'operator' => 'IN'
),
));
$wine_type_query = null;
$wine_type_query = new WP_Query($args);
if( $wine_type_query->have_posts() ) : ?>
<li id="wine-type">
<h2><?php echo $tax->name; ?></h2>
<ul>
<?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
<li class="wine-item">
<h3><?php the_title(); ?></h3>
<span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
<span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
<span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
<span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
<div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
<?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
</li>
<?php endwhile;?>
</ul>
</li>
<?php endif; wp_reset_query(); ?>
<?php } ?>
</ul><!--/.products-->
<?php }
I have this code which has taken over 7 hours to put together through research but the last hours or so I have hit a brick wall!
I require the taxonomy title to be hidden if there are no posts associated with it.
Here is the page: http://cb.dannycheeseman.me/wine-menu/usa/california/
Here is my code:
/**********************************************/
// CUSTOM WINE MENU SHORTCODE
/**********************************************/
add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );
function wine_list_per_cat($atts){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cat' => '', // list of categories in the format 0,1,2,3,4
'tax' => 'product_cat', // taxonomy use
'per_cat' => '3', // max featured items to display per category
'columns' => '3', // columns size
), $atts));
?>
<ul id="wine-menu">
<?php
$wine_type_terms = get_terms( 'wine-type' );
foreach ( $wine_type_terms as $wine_type_term ) {
$args = array(
'post_type' => 'product',
'showposts' => -1,
'tax_query' => array(
'relation' => 'IN',
array(
'taxonomy' => 'wine-type',
'field' => 'slug',
'terms' => array( $wine_type_term->slug ),
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat
)
));
$wine_type_query = new WP_Query($args);?>
<li>
<h2><?php echo $wine_type_term->name; ?></h2>
<ul>
<?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
<li class="wine-item">
<h3><?php the_title(); ?></h3>
<span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
<span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
<span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
<span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
<div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
<?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
</li>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
</ul>
</li>
<?php } ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
<?php }
UPDATE! OK!!! Woooohooo! Found out what it was.. I should have been calling if have posts before the call for $tax->name; Here is my final, and working code! :)
/**********************************************/
// CUSTOM WINE MENU SHORTCODE
/**********************************************/
add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );
function wine_list_per_cat($atts){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cat' => '', // list of categories in the format 0,1,2,3,4
'tax' => 'product_cat', // taxonomy use
'per_cat' => '3', // max featured items to display per category
'columns' => '3', // columns size
), $atts));
?>
<ul id="wine-menu">
<?php
$taxonomy = get_terms( 'wine-type', array('hide_empty' => true, 'pad_counts' => true));
foreach ( $taxonomy as $tax ) {
$args = array(
'post_type' => 'product',
'orderby' => 'title',
'showposts' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat,
'operator' => 'IN'
),
array(
'taxonomy' => 'wine-type',
'field' => 'slug',
'terms' => array( $tax->slug ),
'operator' => 'IN'
),
));
$wine_type_query = null;
$wine_type_query = new WP_Query($args);
if( $wine_type_query->have_posts() ) : ?>
<li id="wine-type">
<h2><?php echo $tax->name; ?></h2>
<ul>
<?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
<li class="wine-item">
<h3><?php the_title(); ?></h3>
<span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
<span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
<span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
<span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
<div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
<?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
</li>
<?php endwhile;?>
</ul>
</li>
<?php endif; wp_reset_query(); ?>
<?php } ?>
</ul><!--/.products-->
<?php }
Share
Improve this question
edited Apr 4, 2014 at 1:55
user49899
asked Apr 4, 2014 at 0:14
user49899user49899
193 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0UPDATE! OK!!! Woooohooo! Found out what it was.. I should have been calling if have posts before the call for $tax->name; Here is my final, and working code! :)
<?php
/**********************************************/
// CUSTOM WINE MENU SHORTCODE
/**********************************************/
add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );
function wine_list_per_cat($atts){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cat' => '', // list of categories in the format 0,1,2,3,4
'tax' => 'product_cat', // taxonomy use
'per_cat' => '3', // max featured items to display per category
'columns' => '3', // columns size
), $atts));
?>
<ul id="wine-menu">
<?php
$taxonomy = get_terms( 'wine-type', array('hide_empty' => true, 'pad_counts' => true));
foreach ( $taxonomy as $tax ) {
$args = array(
'post_type' => 'product',
'orderby' => 'title',
'showposts' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat,
'operator' => 'IN'
),
array(
'taxonomy' => 'wine-type',
'field' => 'slug',
'terms' => array( $tax->slug ),
'operator' => 'IN'
),
));
$wine_type_query = null;
$wine_type_query = new WP_Query($args);
if( $wine_type_query->have_posts() ) : ?>
<li id="wine-type">
<h2><?php echo $tax->name; ?></h2>
<ul>
<?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
<li class="wine-item">
<h3><?php the_title(); ?></h3>
<span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
<span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
<span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
<span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
<div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
<?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
</li>
<?php endwhile;?>
</ul>
</li>
<?php endif; wp_reset_query(); ?>
<?php } ?>
</ul><!--/.products-->
<?php }
本文标签: custom taxonomyHiding taxonomies with no children WPQuery amp taxquery
版权声明:本文标题:custom taxonomy - Hiding taxonomies with no children WP_Query & tax_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282346a1926615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
hide_empty
argument forget_terms
is to not show terms without posts associated with them. – s_ha_dum Commented Apr 4, 2014 at 0:22