2016-11-15 144 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 
); 

和它的作品般的魅力。