简体   繁体   English

从 WP 查询中排除特定的 Woocommerce 产品类别术语

[英]Exclude specific Woocommerce product category term(s) from a WP Query

I'm trying to load latest product in Woocommerce (on home page).我正在尝试在 Woocommerce(在主页上)加载最新产品。 I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.我想从循环中的显示中排除特定的产品类别,但排除产品类别 ID 对我不起作用。

What I have done so far:到目前为止我做了什么:

<?php  
    $args = array(
    'post_type'             => 'product',   
    'posts_per_page'        => 12,
    'post_status'           => 'publish',
    'taxonomy' => 'product_cat',
    'exclude'    => 29,//exclude mention category id from loop
    'parent'   => 0

    );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
?>

    <div class="popular-inner">
        <a href="<?php echo get_permalink(); ?>">

            <div class="popular-image d-flex ">
                <div class="align-self-center mx-auto ">
                    <?php the_post_thumbnail(); ?>
                </div>

            </div>

            <div class="popular-content-wp">
            <div class="popular-title">
                <h6><?php echo get_the_title(); ?></h6>
            </div>
            <div class="popular-price">
                <p><?php echo wc_price($product->get_price()); ?></p>
            </div>
            <div class="popular-add-to-cart">
                <ul>
                    <li>
                        <a href="<?php echo get_permalink(); ?>" class="dodo">Add to Cart</a>
                    </li>
                </ul>
            </div>
            </div>
        </a>    
    </div>

<?php endwhile; wp_reset_query();?>

Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query .因为使用在一个分类蛞蝓WordPress版本3.1 WP_Query在风味已弃用tax_query Also there are some other mistakes.此外还有一些其他错误。 See WP_Query taxonomy parameters .请参阅WP_Query分类参数

Your revisited code:您重新访问的代码:

<?php
    $loop = new WP_Query( array(
        'post_type'      => 'product',
        'posts_per_page' => 12,
        'post_status'    => 'publish',
        'parent'         => 0,
        'tax_query'      => array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => array( 29 ), // Term ids to be excluded
            'operator' => 'NOT IN' // Excluding terms
        ) ),
    ) );

    if ( $loop->have_posts() ) :

    while ( $loop->have_posts() ) : $loop->the_post();

    // Get the instance of the WC_Product from the post ID
    $product = wc_get_product( get_the_id() );
?>
    <div class="popular-inner">
        <a href="<?php echo get_permalink(); ?>">

            <div class="popular-image d-flex ">
                <div class="align-self-center mx-auto ">
                    <?php the_post_thumbnail(); ?>
                </div>

            </div>

            <div class="popular-content-wp">
            <div class="popular-title">
                <h6><?php echo get_the_title(); ?></h6>
            </div>
            <div class="popular-price">
                <p><?php echo wc_price( $product->get_price() ); ?></p>
            </div>
            <div class="popular-add-to-cart">
                <ul>
                    <li>
                        <a href="<?php echo get_permalink(); ?>" class="dodo">Add to Cart</a>
                    </li>
                </ul>
            </div>
            </div>
        </a>
    </div>
<?php
    endwhile;
    wp_reset_postdata();

    else: ?>
    <p><?php _e( 'No posts found.' ); ?></p>

    <?php endif;
?>

It should work now.它现在应该可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从WP_Query中排除WooCommerce产品类别 - Exclude a WooCommerce product category from a WP_Query 从 WP_Query 中的当前产品类别术语 ID 获取所有 Woocommerce 产品 - Get all Woocommerce products from current product category term Id in a WP_Query 从产品标签内的特定产品类别中排除Woocommerce产品 - Exclude Woocommerce products from a specific product category inside product tags 从 Woocommerce 的产品类别小部件中排除特定术语 - Exclude a specific term from product categories widget in Woocommerce 在 WP_Query 中获取 WooCommerce 订阅产品类型和特定类别 - Get WooCommerce Subscription product type and specific category in a WP_Query 从 WooCommerce 中的类别价格后缀更改中排除特定产品 ID - Exclude specific product ids from a category price suffix change in WooCommerce 从Woocommerce相关产品中排除特定产品类别 - Exclude specific product category from Woocommerce related products 从WooCommerce中的相关产品中排除产品类别 - Exclude a product category from Related products in WooCommerce 从 Woocommerce 相关产品中排除产品类别 - Exclude a product category from Woocommerce related products 从子类别术语中定位 WooCommerce 产品类别存档页面 - Target WooCommerce product category archive pages from a child category term
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM