【发布时间】:2020-12-25 17:03:11
【问题描述】:
在作者存档页面上,我想显示作者发布的每个分类(在我的案例产品类别中)(在我的案例中是 WooCommerce 产品)。
为此,我使用以下代码:
$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$author_categories = array();
//loop over the posts and collect the terms
foreach ($posts as $p) {
$author_categories = wp_get_object_terms( $p->ID, 'product_cat');
if ( ! empty( $author_categories ) && ! is_wp_error( $author_categories ) ){
echo '<div class="d-flex flex-wrap">';
foreach ($author_categories as $author_category) {
//var_dump($t);
$author_category_link = get_term_link( $author_category );
$author_category_name = $author_categories[] = $author_category->name;
echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
echo $author_category_name;
echo '</a>';
}
echo '</div>';
}
}
wp_reset_postdata();
问题是,产品类别出现多次。我猜是该产品类别中的每个帖子。
有什么方法可以先收集条款,然后按产品类别中的帖子数排序后仅显示一次?
【问题讨论】:
标签: php wordpress woocommerce categories custom-taxonomy