【问题标题】:Wordpress related products loop hide current productWordpress 相关产品循环隐藏当前产品
【发布时间】:2016-03-08 16:11:26
【问题描述】:

查看某个产品时,我正在显示最新产品。查看产品时,它仍会在底部显示为“最近的产品”。

<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) ,但它不起作用。

如何从最近的产品循环中隐藏我正在查看的产品?

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    您忘记将$product 声明为global,但global $post; $post-&gt;IDglobal $product; $product-&gt;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]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2015-07-27
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多