2016-11-15 149 views
2

我更改了商店頁面分頁並添加了自定義代碼以在頁面滾動中使用ajax加載產品。我的網站是多語言的,我正在使用WPML。該代碼在默認語言下工作正常,但使用其他語言,而不是顯示該語言的產品,它向我顯示默認語言即英語的產品。意思是我想展示翻譯產品對應於特定語言。如何使用WP_Query獲取當前語言的woocommerce產品?

這裏是我的代碼Ajax代碼:

var $ = jQuery; 
var flag=1; 
var limit=0; 
$(window).scroll(function(){ 
     if($(window).scrollTop() == $(document).height() - $(window).height()){ 
      limit++; 
      if(flag==1) 
      { 
       $(".lazy_lode_img").css('display','block');//display loading image 
       $.ajax({ 
        url:"<?php echo get_stylesheet_directory_uri();?>/ajax.php", // ajax page 
        type:'POST', 
        data:{ 'paged':limit}, // send page no 
        dataType:"html", 
        success: function(product_data){ 
         //alert(product_data); 
         if(product_data!=0) 
         { 
          $(".shop-products.row.grid-view").append(product_data); 
          $(".lazy_lode_img").css('display','none'); 
         } 
         else 
         { 
          flag=0; 
          // $('#k_test').append('<div class="news" id="no_news" style="text-align:center;">NO MORE PRODUCT</div>'); 
          $(".lazy_lode_img").remove(); 
         } 
        } 
       }); 
      } 
     } 
    }); 

這是我的PHP代碼:

$page_no = $_POST['paged']; 
$post_per_page=6; 
$args = array(
    'posts_per_page' => $post_per_page,//set post per page 
    'paged'   => $page_no,//set offset for limit 
    'post_type' => 'product', 
    'post_status'=>'publish', 
); 

$query = new WP_Query($args); 
if ($query->have_posts()) { 
    while ($query->have_posts()) : $query->the_post(); 
     /*$price = get_post_meta($query->post->ID, '_regular_price', true); 
     $price=round($price,2); 
     if ((int) $price == $price) 
     { 
      $price=$price.'.00'; 
     }*/ 
     ?> 
     <div class="item-col col-xs-6 col-md-4 col-sm-4 post-6303 product type-product status-publish has-post-thumbnail product_cat-all-products product_cat-tarts instock shipping-taxable purchasable product-type-simple"> 
      <div class="product-wrapper product-wrapper2"> 
       <div class="list-col4"> 
        <div class="product-image"> 
         <a class="twoimg" href="<?php echo get_permalink($query->post->ID);?>" title="Blueberry Frangipane Tart"> 
          <?php echo get_the_post_thumbnail($query->post->ID, 'shop_catalog');?> 
         </a> 
        </div> 
        <div class="home-product-title"> 
         <h2 class="product-name"> 
          <a href="<?php echo get_permalink($query->post->ID);?>" style="color:black;"><?php echo $query->post->post_title;?></a> 
         </h2> 
         <span class="arrow-img"></span> 
        </div> 
       </div> 
       <div class="clearfix"></div> 
      </div> 
     </div> 
     <?php 
    endwhile; 
} 
else 
{ 
    echo '0'; 
} 

我不明白的地方,我應該通過語言的代碼,以便將取回的產品當前選定的語言在前端。

+0

您是否閱讀過這個https://wpml.org/forums/topic/show-default-languageplugin-if-not-translated/ –

+0

我試過使用switch_lang(),但它仍然顯示我英文版的產品。 。 –

回答

1

花費時間後,終於找到了解決方案。

我只是增加了一個參數

'lang' => $current_language 

現在我的查詢參數的樣子:

$args = array(
    'posts_per_page' => $post_per_page,//set post per page 
    'paged'   => $page_no,//set offset for limit 
    'post_type' => 'product', 
    'post_status'=>'publish', 
    'lang' => $current_language 
); 

和它的作品般的魅力。