首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单一产品页面上增加相关产品(woocommerce)

在单一产品页面上增加相关产品(woocommerce)
EN

Stack Overflow用户
提问于 2014-07-07 16:00:30
回答 1查看 3.7K关注 0票数 0

我正在做一些工作在一个网站运行wordpress - woocommerce风格的木星主题。

问题是,我已经向开发人员寻求帮助,但这已经有一段时间了。

我正在寻找一个方法,以增加在底部的“相关产品”从2到4,所以它填写模板。

在related.php中,我认为我找到了改变相关产品数量的值,但我错了。它什么也不做,对我来说这本身就很奇怪。

年长的

代码语言:javascript
复制
 <?php
/**
 * Related Products
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product, $woocommerce_loop;

$related = $product->get_related( $posts_per_page );

if ( sizeof( $related ) == 0 ) return;

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

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $columns;

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

    <div class="related products">

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

        <?php woocommerce_product_loop_start(); ?>

            <?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;

wp_reset_postdata();

新的

代码语言:javascript
复制
<?php
/**
 * Related Products
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

 global $product, $woocommerce_loop;

 $related = $product->get_related( 6 );

 if ( sizeof( $related ) == 0 ) return;

 $args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'ignore_sticky_posts'  => 1,
    'no_found_rows'        => 1,
    'posts_per_page'       => 6,
    'orderby'              => $orderby,
    'post__in'             => $related,
    'post__not_in'         => array( $product->id )
 ) );

 $products = new WP_Query( $args );

 $woocommerce_loop['columns'] = 4;

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

    <div class="related products">

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

        <?php woocommerce_product_loop_start(); ?>

            <?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;

 wp_reset_postdata();

所以我现在真的很困惑,希望任何人都有关于如何改变它的建议或解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-07 16:07:31

您需要重新定义一个WooCommerce函数,以覆盖它包含的默认值:

在您的functions.php中添加以下内容:

代码语言:javascript
复制
// Redefine woocommerce_output_related_products()
function woocommerce_output_related_products() {
     woocommerce_related_products(4,2); // Display 4 products in rows of 2
}

您需要编辑括号中的数字以满足您的要求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24614873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档