隐藏没有子项的分类WP_QUERY和TAX_QUERY

时间:2014-04-04 作者:user49899

我有这个代码,通过研究花了7个多小时才完成,但在过去的几个小时左右,我遇到了麻烦!

如果没有与分类法标题相关的帖子,我要求将其隐藏。

页面如下:http://cb.dannycheeseman.me/wine-menu/usa/california/

这是我的代码:

/**********************************************/
//  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 } 

1 个回复
SO网友:user49899

使现代化好啊呜呜呜!找到了那是什么。。如果在调用$tax->name之前有帖子,我应该打电话;这是我的最终工作代码!:)

<?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 } 

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register