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


Increase related products on single product page (woocommerce)

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

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

我正在寻找一种方法将底部的"相关产品"从2增加到4,以便填满模板。

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

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

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

所以我现在真的很困惑,希望谁有建议或解决方案如何改变它

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

在你的functions.php中添加:

// Redefine woocommerce_output_related_products()
function woocommerce_output_related_products() {
     woocommerce_related_products(4,2); // Display 4 products in rows of 2
}

你需要编辑括号中的数字来满足你的要求。