Php 从WP查询中排除特定的Woocommerce产品类别术语
我正在尝试加载Woocommerce中的最新产品(在主页上)。我想从循环显示中排除特定的产品类别,但排除产品类别id对我不起作用 到目前为止我所做的:Php 从WP查询中排除特定的Woocommerce产品类别术语,php,wordpress,woocommerce,product,taxonomy-terms,Php,Wordpress,Woocommerce,Product,Taxonomy Terms,我正在尝试加载Woocommerce中的最新产品(在主页上)。我想从循环显示中排除特定的产品类别,但排除产品类别id对我不起作用 到目前为止我所做的: <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'post_status' => 'publish', 'taxonomy
<?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();?>
因为wordpress 3.1版在a中使用分类法slug在tax\u查询的风格中被弃用。还有其他一些错误。看
您的重新访问代码:
<?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;
?>
现在应该可以用了。很好,我不知道有什么变化,请原谅,我很感谢你指出我的错误,这对我很有帮助。。。。