Wordpress 如何在商业中以随机顺序获得所有产品及其变化

Wordpress 如何在商业中以随机顺序获得所有产品及其变化,wordpress,woocommerce,Wordpress,Woocommerce,我有一个WooCommerce商店,里面有各种各样的产品,我想在商店页面上展示所有的产品及其各种各样的产品。 我的代码如下: $params = array('posts_per_page' => 2, 'post_type' => 'product', 'orderby' => 'rand'); $wc_query = new WP_Query($params); $paged = (get_query_var('paged')) ? get_query_var('page

我有一个WooCommerce商店,里面有各种各样的产品,我想在商店页面上展示所有的产品及其各种各样的产品。 我的代码如下:

$params = array('posts_per_page' => 2, 'post_type' => 'product', 'orderby' => 'rand');

$wc_query = new WP_Query($params);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(
    'post_type'      => 'product',
    'paged'          => $paged,
    'posts_per_page' => 2,
    'orderby' => 'rand'
));

if (have_posts()) : 
while (have_posts()) : the_post();
echo '<h2>'.the_title.'</h2>';
if ($product->is_type( 'variable' )) 
{
    $available_variations = $product->get_available_variations();
    foreach ($available_variations as $key => $value){
        if($value['variation_is_active']==1){
            // varible products details
        }
    }
}
endwhile; 
  the_posts_pagination();
  wp_reset_postdata();
else:
  echo 'No Products';
endif:
$params=array('posts\u per\u page'=>2,'post\u type'=>'product','orderby'=>'rand');
$wc_query=新的WP_查询($params);
$paged=(获取查询变量('paged'))?获取查询变量('paged'):1;
查询职位(数组)(
“post_类型”=>“产品”,
“paged”=>$paged,
“每页帖子数”=>2,
'orderby'=>'rand'
));
如果(have_posts()):
while(have_posts()):the_post();
回显“.”标题“;
如果($product->is_类型('variable'))
{
$available_variations=$product->get_available_variations();
foreach($key=>$value){
如果($value['variation\u处于活动状态]==1){
//各种产品详情
}
}
}
结束时;
_posts_pagination();
wp_reset_postdata();
其他:
回应“没有产品”;
endif:
//我得到的产品,但只是随机的主要产品,而不是变化

请帮帮我。 提前谢谢

要获得可变产品,您必须在中使用
product\u variation
post\u类型

因此,您的
$params
查询帖子应该如下所示:

$params = array(
    'posts_per_page' => 2,
    'post_type' => array('product', 'product_variation'), // <-- check this line.
    'orderby' =>
    'rand'
);
//...
//...
query_posts(array(
    'post_type' => array('product', 'product_variation'), // <-- check this line.
    'paged' => $paged,
    'posts_per_page' => 2,
    'orderby' => 'rand'
    )
);
$params=array(
“每页帖子数”=>2,
“post_type”=>数组(“产品”、“产品变体”),//
“兰德”
);
//...
//...
查询职位(数组)(
“post_type”=>数组(“产品”、“产品变体”),//$paged,
“每页帖子数”=>2,
'orderby'=>'rand'
)
);
参考:

希望这有帮助