2017-11-29 9 views
0

私は店のすべての製品にwhileでループしようとしています。これは、 "関連製品"機能を変更することです。しかし、それはわずか6回実行されますが、私は49の製品を持っています。すべての製品のワードプレスループ

私は理由を理解できますか?

おかげ

if (is_singular('product')) { 
    global $post; 
    // get categories 
    $terms = wp_get_post_terms($post->ID, 'product_cat'); 

    $title = get_the_title($post); 


    $pattern = '/[^a-zA-Z0-9 ]/'; 
    // $pattern = '/ /'; 
    $matches = array(); 
    $dummy = preg_match($pattern, $title, $matches); 
    $posi = strpos($title, $matches[0]); 
    // print_r($posi); 
    $productname = substr($title, 0, $posi-1); 
    // print_r($productname); 
    $productcolor = substr($title, $posi+3); 
    // print_r($productcolor); 



    foreach ($terms as $term) $cats_array[] = $term->term_id; 
    $query_args = array('orderby' => 'title', 
         'post__not_in' => array($post->ID), 
         'posts_per_page' => 200, 
         'no_found_rows' => 10, 
         'post_status' => 'publish', 
         'post_type' => 'product', 
         'tax_query' => array(
               array(
                'taxonomy' => 'product_cat', 
                'field' => 'id', 
                'terms' => $cats_array 
               ) 
              ) 
         ); 
    $r = new WP_Query($query_args); 


    if ($r->have_posts()) { ?> 

     <div class="related products"> 
      <h2><?php _e('Related Products', 'woocommerce'); ?></h2> 

      <?php woocommerce_product_loop_start(); ?> 

       <?php 
        $counter = 0; 
        while ($r->have_posts()) : 
         $r->the_post(); 
         global $product; 
         $counter = $counter + 1; 


         wc_get_template_part('content', 'product'); 


        endwhile; // end of the loop. 
        print_r($counter) 
       ?> 

      <?php woocommerce_product_loop_end(); ?> 

     </div> 

     <?php 

     wp_reset_query(); 
    } 
} 

答えて

1

ない、それが適用される場合は必ず、私はちょうどここで見つけることができます関連製品テンプレート、変更をお勧めします:

  1. 作成します。これを行うにはhttps://github.com/woocommerce/woocommerce/blob/master/templates/single-product/related.php

    をテーマルートの中の "wooocommerce"フォルダ。

  2. /wp-content/plugins/woocommerce/woocommerce/templates/single-product/related.php
  3. からrelated.phpファイルをコピーし、新しいファイルの場所は、/ WP-コンテンツ/あなたをテーマにする必要があります/wooocmmerce/single-product/related.php
  4. 上記のファイルを修正します。

また、これは達成しようとしていることに完全に依存します。私はあなたのコードを編集するのが嬉しいですが、何を変更したいのかを知る必要があるので、重要なものは削除しないでください。

+0

私は実際にrelated.phpファイルを変換するのに苦労しました。私は戦略を変更し、functions.phpに直接コードを書きました。ご協力いただきありがとうございます ! – Louis

関連する問題