我在查看某个产品时正在显示最新产品。查看产品时,它仍然在底部显示为"最近使用的产品"。
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'post__in' => $related,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
我尝试使用"post__not_in"=> array($product->id),但它不起作用。
如何在最近的产品循环中隐藏我正在查看的产品?
你忘了将$product
声明为 global
但global $post; $post->ID
和global $product; $product->id
都可以。另外,请注意post__not_in
如果如果在与post__in相同的查询中使用此功能,则将忽略它
它在那里(我假设是这样),您将需要从$related中删除您当前的产品 ID
$key=array_search($post->ID,$related);
//or
$key=array_search($product->id, $related);
if($key!==FALSE)
{
unset($related[$key]);
}