从输出中排除特定类别ID

问题描述

我的网站上有以下代码,用于输出“下载”帖子类型的类别列表。我希望排除某些类别以使其不出现在输出中,我该怎么做?

<?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 );

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...