2015-10-02 147 views
1

我已經有了相關產品成立,但它只是顯示2.我需要它橫跨顯示5,但沒有代碼,我已經調整了改變任何東西。WooCommerce相關產品只顯示2個產品

這是我的related.php代碼:

<?php 

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

global $product, $woocommerce_loop; 

if (empty($product) || ! $product->exists()) { 
    return; 
} 

$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'  => 5, 
    '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(); 

,這是對單一產品頁面模板的相關產品代碼:

<div class="row"> 
       <div class="col-md-12"> 
        <?php echo do_shortcode ('[related_products per_page="5"]'); ?> 
       </div> 
       </div> 
+0

我認爲這是關係到過濾器'woocommerce_related_products_args',它可能改變'posts_per_page'值。如果這個過濾器有任何定義,你可以檢查你的主題嗎? – vard

+0

定義的唯一'posts_per_page'是在我粘貼在related.php上面的代碼中,它被設置爲5. – graphixg1

+0

我在說過濾器['woocommerce_related_products_args'](http://hookr.io/filters/woocommerce_related_products_args/ ) - 搜索它而不是'posts_per_page'(filter參數可以用其他方式命名)。 – vard

回答

0

你需要創建一個woocomerce子頁和連接對此。 'posts_per_page'=> 5,無法控制當前顯示的內容。 woocomerce頁面處理它。

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

然後,如果您使用的是子主題創建一個名爲「woocommerce」文件夾,將一個名爲「similarProductsFive.php」它裏面的文件,或者將孩子woocommerce文件夾你的主題內部的文件。

然後將此代碼添加到similarProuctFive.php

<?php 
/** 
* The template for displaying product content within loops. 
* 
* Override this template by copying it to yourtheme/woocommerce/content-product.php 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  1.6.4 
*/ 

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

global $product, $woocommerce_loop; 

// Store loop count we're currently on 
if (empty($woocommerce_loop['loop'])) 
    $woocommerce_loop['loop'] = 0; 

// Store column count for displaying the grid 
if (empty($woocommerce_loop['columns'])) 
    $woocommerce_loop['columns'] = apply_filters('loop_shop_columns', 4); 

// Ensure visibility 
if (! $product || ! $product->is_visible()) 
    return; 

// Increase loop count 
$woocommerce_loop['loop']++; 

// Extra post classes 
$classes = array(); 
if (0 == ($woocommerce_loop['loop'] - 1) % $woocommerce_loop['columns'] || 1 == $woocommerce_loop['columns']) 
    $classes[] = 'first'; 
if (0 == $woocommerce_loop['loop'] % $woocommerce_loop['columns']) 
    $classes[] = 'last'; 
    $classes[] = "store-similar-products product"; 
?> 
<li <?php post_class($classes); ?>> 

    <?php do_action('woocommerce_before_shop_loop_item'); ?> 

    <a href="<?php the_permalink(); ?>"> 

     <?php 
      /** 
      * woocommerce_before_shop_loop_item_title hook 
      * 
      * @hooked woocommerce_show_product_loop_sale_flash - 10 
      * @hooked woocommerce_template_loop_product_thumbnail - 10 
      */ 
      do_action('woocommerce_before_shop_loop_item_title'); 
     ?> 

     <h3><?php the_title(); ?></h3> 

     <?php 
      /** 
      * woocommerce_after_shop_loop_item_title hook 
      * 
      * @hooked woocommerce_template_loop_rating - 5 
      * @hooked woocommerce_template_loop_price - 10 
      */ 
      do_action('woocommerce_after_shop_loop_item_title'); 
     ?> 

    </a> 

    <?php 

     /** 
     * woocommerce_after_shop_loop_item hook 
     * 
     * @hooked woocommerce_template_loop_add_to_cart - 10 
     */ 
     do_action('woocommerce_after_shop_loop_item'); 

    ?> 

</li> 

然後,您可以更改產品的數量顯示在這裏:

$woocommerce_loop['columns'] = apply_filters('loop_shop_columns', 4) 
相關問題