1 回答
TA贡献1784条经验 获得超8个赞
你可以试试这个方法。我检查了它是否有效:
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
$first_term = array_shift( $terms );
$args = array(
'posts_per_page' => 4,
'orderby' => 'rand',
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( $first_term->slug )
)
),
'post__not_in' => array( $product->get_id() )
);
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();
?>
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报