【发布时间】:2018-01-26 03:58:22
【问题描述】:
我正在尝试显示不同产品类别的随机产品图片。将它们链接到类别页面并获取类别标题。我的代码得到了随机图像,但是太多了,不知道如何获取类别 slug 和标题。
$args = array(
'taxonomy' => 'product_cat',
'posts_per_page' => -1,
'showposts' => -1,
'numberposts' => -1,
'orderby' => 'rand',
);
$the_query = new WP_Query( $args );
while ($the_query->have_posts()) : $the_query->the_post();
$temp_thumb = get_the_post_thumbnail($post->ID, 'shop_thumbnail', array('class' => 'attachment-shop_catalog size-shop_catalog wp-post-image'));
$temp = get_term($post->ID, 'product_cat');
$temp_title = $temp->name;
$temp_url = $temp->slug;
echo '<a href="' . $temp_url . '">' . $temp_thumb . $temp_title . '</a>';
endwhile;
【问题讨论】:
标签: php wordpress woocommerce