我创建了两个自定义分类法(1。Business location 和2。Business Category) 对于自定义帖子类型(Business Listing). 现在我已经列出了下面的所有位置Location taxonomy archive 我列出了所有位置以及用户单击的时间Location A 用户需要发布存档页,共页Location A 其中列出了该特定分类法中的所有帖子。
我想在这里列出每一个Business Category 上面和下面都是Business Category
示例:域中。com/位置/位置1/
Category 1
Post 1
Post 2
Post 3
Category 2
Post 1
Post 2
Post 3
我如何做到这一点?我尝试了几个选项,但下面的代码从所有位置获取帖子,但我想列出当前位置的类别和帖子。
<?php
$taxonomy = \'business-category\';
$args = array(
\'orderby\' => \'id\',
\'order\' => \'ASC\',
);
$taxonomy_terms = get_terms($taxonomy, $args);
if($taxonomy_terms) {
foreach($taxonomy_terms as $taxonomy_term) {
$args = array(
"$taxonomy" => $taxonomy_term->slug,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<h2><?php echo $taxonomy_term->name; ?></h2>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif;
}
}
?>
Answer Modified from Ben HartLenn <?php
$business_location = get_queried_object();
$business_categories = get_terms( [
\'taxonomy\' => \'business-category\',
\'hide_empty\' => true,
] );
$duplicates = [];
foreach ( $business_categories as $business_category ) :
?>
<?php $args = [
\'post_type\' => \'business-listing\',
\'tax_query\' => [
\'relation\' => \'AND\',
[
\'taxonomy\' => \'business-location\',
\'field\' => \'slug\',
\'terms\' => $business_location->slug,
],
[
\'taxonomy\' => \'business-category\',
\'field\' => \'slug\',
\'terms\' => $business_category->slug,
],
],
\'post__not_in\' => $duplicates,
];
$query = new WP_Query( $args );
?>
<div class="business-listing-block">
<?php if ( $query->have_posts() ) : ?>
<h2 class=\'business-category-title\'><?php echo $business_category->name; ?></h2>
<ul class=\'business-listings\'>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( ! in_array( $post->ID, $duplicates ) ) {
$duplicates[] = $post->ID;
} ?>
<li class=\'business-listing\'><?php echo the_title(); ?> </li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>