WordPress相关产品循环隐藏当前产品


Wordpress related products loop hide current product

我在查看某个产品时正在显示最新产品。查看产品时,它仍然在底部显示为"最近使用的产品"。

<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声明为 globalglobal $post; $post->IDglobal $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]);
}