gpt4 book ai didi

php - 从 Woocommerce 中的循环中排除产品类别

转载 作者:行者123 更新时间:2023-12-04 16:44:11 27 4
gpt4 key购买 nike

我想从循环中排除我当前帖子的类别。通常很容易,这次它不起作用,我不知道这里出了什么问题。

这是我的页面代码:

$postid = get_the_ID(); // curret product ID

<section class="related products">

<?php
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'cat' => '-33' // Exclude cat


);

$related_products = new WP_Query($args);

?>


<h2><?php esc_html_e( 'Related products' ); ?></h2>



<div class="owl-carousel rigid-owl-carousel" >


<?php if( $related_products->have_posts() ) {

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


wc_get_template_part( 'content', 'product' );


endwhile; }



?>



</section>

页面结束

 <?php 

wp_reset_postdata();

?>

它显示了所有产品(显示的产品除外,这是正确的)。

你有什么建议吗?

最佳答案

尝试使用以下附加 tax_query,因为产品类别是自定义分类法:

<?php
$related_products = new WP_Query( array(
'post__not_in' => array( get_the_ID() ), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 33 ), // HERE the product category to exclude
'operator' => 'NOT IN',
) ),
) );

if( $related_products->have_posts() ) : ?>

<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
?>
</div>
<?php endif; ?>

关于php - 从 Woocommerce 中的循环中排除产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52112936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com