我有一个循环都显示不同类别去存档页面上。
Ÿ如何只能显示类别之一的时间?
目前,如果两个职位有相同的类别中,有两个时间范畴
这是我下面的代码
<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>
方法1:排除来自WordPress的使用,你需要做插件的第一件事一类是安装并激活终极类别逃生插件。欲了解更多详情,您应该遵循我们对如何安装WordPress插件指南。
激活后,你就需要去设置»类别逃生页。它会显示所有可用上你的WordPress博客的类别。
方法2:排除来自WordPress的首页使用代码类别此方法要求您将代码添加到你的WordPress文件。如果你还没有这样做之前,然后看看我们如何在WordPress的复制和粘贴代码片断指南。
您将需要添加以下代码到你的主题的functions.php文件或特定网站的插件。
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
不要忘了你的类ID更换ID(-5)。它会隐藏网页属于这个ID匹配的类别中的所有博客文章。
注意:请务必添加一个减号( - )标志的类别ID。
请参考:https://www.wpbeginner.com/wp-tutorials/how-to-exclude-a-category-from-your-wordpress-homepage/
如果我没有理解你的问题,你可以介绍你会记得已经使用什么类型的变量,所以你不把他们不止一次。
<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();
$categories_displayed = [];
$separator = ", ";
$output = ' ';
if ($categories) {
foreach ($categories as $category) {
if (!in_array($category->cat_name, $categories_displayed)) {
$output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';
$categories_displayed[] = $category->cat_name;
}
}
echo trim($output, $separator);
}
?>
</div>
</div>
<?php
} // End while
} // End if
else { echo '<p>Aucune actualité trouvée</p>'; } ?>
<?php wp_reset_postdata(); ?>
</div>