产品循环问题中的头等舱和末班车

时间:2019-06-06 19:23:49

标签: woocommerce

我正在运行一个简单的自定义产品循环。我的问题是LAST类设置在错误的时间。似乎loop_index不正确。我应该每行有4个产品,但是在第3个产品之后设置了最后一个课程。任何想法可能是什么问题?

谢谢

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 40
);

$loop = new WP_Query($args);

if ( $loop->have_posts() ) {
  while ( $loop->have_posts() ) : $loop->the_post();
      wc_get_template_part( 'content', 'product' );
  endwhile;
} else {
  echo __( 'No products found' );
}
wp_reset_postdata();

2 个答案:

答案 0 :(得分:0)

请添加一个过滤器(loop_shop_columns)以覆盖商店列。

function loop_columns() {    
  return 4;   
}

add_filter('loop_shop_columns', 'loop_columns', 999); //overwrite shop column filter

$args = array(
'post_type' => 'product',
'posts_per_page' => 40
);
$loop = new WP_Query($args);

if ( $loop->have_posts() ) {
  while ( $loop->have_posts() ) : $loop->the_post();
      wc_get_template_part( 'content', 'product' );
  endwhile;
} else {
  echo __( 'No products found' );
}
wp_reset_postdata();

确保仅在相关函数中调用add_filter。否则,它将影响所有存档/类别页面。

答案 1 :(得分:0)

woocommerce_reset_loop();

在args之前添加