我创建了两个自定义分类法(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; ?>
最合适的回答,由SO网友:Ben HartLenn 整理而成
大概是你的taxonomy-business-location.php
然后是模板文件。以下是这样一个模板中的主要内容区域的基本情况:
<div id="main-content">
<?php
$business_location = get_queried_object();
echo "<h1 class=\'business-location-title\'>Businesses in " . $business_location->name . ":</h1><hr>"; // Location A, according to your example
$business_categories = get_terms( [
\'taxonomy\' => \'business-category\',
\'hide_empty\' => true, // hide empty business categories(i.e. have no posts)
] );
$duplicates = []; // more on this later...
// start looping through business categories
foreach ( $business_categories as $business_category ) {
// create new query that gets business listings, that are in Location A business location, and are in the current business category from the foreach loop above
$args = [
\'post_type\' => \'business-listing\',
\'tax_query\' => [
\'relation\' => \'AND\', // business listings must have both taxonomies terms queried below
[
\'taxonomy\' => \'business-location\',
\'field\' => \'slug\',
\'terms\' => $business_location->slug,
],
[
\'taxonomy\' => \'business-category\',
\'field\' => \'slug\',
\'terms\' => $business_category->slug,
],
],
\'post__not_in\' => $duplicates, // prevents duplicate business listings from showing, you may or may not want this part, more info below...
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : // don\'t show business category if no posts in current business category and location
echo "<h2 class=\'business-category-title\'>" . $business_category->name . "</h2>";
echo "<ul class=\'business-listings\'>";
// looping through business listings
while ( $query->have_posts() ) : $query->the_post();
/* Collect business listing id\'s and the array gets added
to the next WP_Query\'s post__not_in. This way a business
listing will only display in the first business category
shown, and none of the other business categories that
the listing is in. Again, this may or may not be wanted.
*/
if( !in_array($post->ID, $duplicates) ) {
$duplicates[] = $post->ID;
}
// output business listing
echo "<li class=\'business-listing\'>" . get_the_title() . "</li>";
endwhile;
echo "</ul>";
endif;
}
?>
</div><!-- /#main-content -->
编辑:这是我的商业列表数据设置的图片,如果它对您有帮助,我会得到输出: