【问题标题】:Woocommerce related products section shows the same productsWoocommerce 相关产品部分显示相同的产品
【发布时间】:2020-05-29 14:59:28
【问题描述】:

我想在我的产品页面上显示相关产品部分,但它在所有列中显示实际产品。 这是我的代码:

<?php
    while ($loop->have_posts()): $loop->the_post();
        $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id($product->get_id()), 'single-post-thumbnail'); ?>
        <div class="col-lg-3 related-tour">
            <article>
                <div class="featured-image" style="background-image: url(<?php echo (!empty($featured_image)) ? $featured_image[0] : '';?>);"></div>
                <div class="article-info">
                    <h2>
                        <a href="<?php echo $product->get_permalink();?>"><?php echo $product->get_name();?></a>
                    </h2>
                    <p class="category">
                        <span><?php pll_e( 'Categories:' ); ?></span>
                        <?php echo $product->get_categories(', ', ' ' . __(' ', '  ', $cat_count, 'woocommerce') . ' ', ' ');?>
                    </p>
                </div>
            </article>
        </div>
    <?php endwhile;?>
    <?php wp_reset_query();?>

我应该改变什么来解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce woocommerce-theming


    【解决方案1】:

    不要使用自定义函数来展示相关产品。

    所以函数woocommerce_output_related_products显示相关产品。

    查看此文件内部/single-product/related.php。 (https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/related.php)

    您可以更改使用 woocommerce_output_related_products_args 过滤器的相关产品查询的产品参数。

    add_filter( 'woocommerce_output_related_products_args', 'my_woocommerce_output_related_products_args' );
    function woocommerce_output_related_products_args( $args ) {
        $args['post__not_in'] = [ get_the_ID() ];
    
        return $args;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多