【发布时间】:2019-02-11 09:45:57
【问题描述】:
我有一个循环来显示归档页面上的不同类别。
你怎么能只显示类别一次?
目前,如果两个帖子属于同一类别,则该类别有两次
这是我下面的代码
<div class="row ptb-20">
<?php
$args = array(
'category_name' => 'actualites',
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="category-filter">
<div class="single-filter">
<?php
$categories = get_the_category();
$separator = ", ";
$output = ' ';
if ($categories) {
foreach ($categories as $category) {
$output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';
}
echo trim($output, $separator);
}
?>
</div>
</div>
<?php
} // End while
} // End if
else { echo '<p>Aucune actualité trouvée</p>'; } ?>
<?php wp_reset_postdata(); ?>
</div>
【问题讨论】:
标签: php wordpress categories