问题描述
我的网站上有以下代码,用于输出“下载”帖子类型的类别列表。我希望排除某些类别以使其不出现在输出中,我该怎么做?
<?php
/**
* Generate list of EDD categories to browse
*/
$args = array(
'orderby' => 'name','hierarchical' => 1,'style' => 'none','taxonomy' => 'download_category','hide_empty' => 0,'depth' => 1,'title_li' => '',// 'parent' => 0
);
$categories = get_categories( $args );
if ( $categories ) {
?>
<div class="search-cats">
<div class="search-cat-text">
<?php _e( 'Browse by category: ','checkout' ); ?>
</div>
<nav>
<?php
/**
* Generate list of EDD category links
*/
foreach ( $categories as $category ) {
$link = get_term_link( $category,'download_category' );
echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $category->name . '</a>';
}
?>
</nav>
</div>
<?php } ?>
解决方法
要在您的参数中查找的参数为exclude
,请参见https://developer.wordpress.org/reference/classes/wp_term_query/__construct/,以获取可用于此类型查询的参数的详细信息。
$args = array(
'orderby' => 'name','hierarchical' => 1,'style' => 'none','taxonomy' => 'download_category','hide_empty' => 0,'depth' => 1,'title_li' => '','exclude' => 24,21 /* Array or comma/space-separated string of term IDs
to exclude. If $include is non-empty,$exclude is
ignored. Default empty array. */
);
$categories = get_categories( $args );