与一种产品相关的Wordpress Woocommerce相关产品

时间:2016-10-30 09:07:42

标签: php mysql wordpress

我目前正在使用Wordpress设计一个爱好wordpress网站并实施了WooCommerce。我正在尝试在产品页面内容下显示产品的相关产品,并且正在查看Wordpress的相关功能。我可以看到related_products查询返回单行但当它只返回单个产品时,则以下WooCommerce循环失败:

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

    <div class="related products">

        <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

        <?php woocommerce_product_loop_start(); ?>
                <?php var_dump($products->the_post()); ?>
            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php wc_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>

<?php endif;

显示相关产品&#34;但是对于单个结果,它不会显示内容,对于多个结果,会显示行。

上面的WooCommerce循环是否存在问题,或者我应该查看查询?

由于

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题,我对过滤器的论点不准确,将它们更新到下面并且有效:

$args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'posts_per_page'       => 14,
    'post__in'             => $related,
    'post__not_in'         => array( $product->id )
) );