You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在EDD分类列表中排除指定分类ID?

如何排除EDD自定义文章类型的特定分类不显示

嘿,我来帮你搞定这个问题!你现在需要在输出EDD的download_category分类列表时排除特定分类,有两种实用的方法可以实现,我给你一步步拆解:

方法1:直接在查询参数中过滤(推荐)

这种方法效率更高,因为它直接在数据库查询阶段就排除了不需要的分类,不用在循环里再额外处理。你只需要在$args数组里添加exclude参数,把要排除的分类ID放进去就行。

比如你要排除ID为12和34的分类,修改后的代码如下:

<?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' => '',
 'exclude' => array(12, 34), // 这里添加要排除的分类ID
 // '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 } ?>

小贴士:你可以在WordPress后台的分类编辑页面,从URL里找到分类的ID(比如URL里的tag_ID=12,12就是分类ID)。

方法2:在循环中跳过特定分类

如果因为某些原因你不想修改查询参数,也可以在遍历分类的循环里添加判断,遇到要排除的分类就直接跳过。

代码示例:

<?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 );
// 定义要排除的分类ID数组
$exclude_cats = array(12, 34);
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 ) {
 // 判断当前分类是否在排除列表里,是就跳过
 if (in_array($category->term_id, $exclude_cats)) {
     continue;
 }
 $link = get_term_link( $category, 'download_category' );
 echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $category->name . '</a>';
 } ?>
 </nav>
 </div>
<?php } ?>

两种方法都能达到目的,优先推荐第一种,因为它减少了不必要的数据查询和循环处理,性能更好~

内容的提问来源于stack exchange,提问作者Teege

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:57:32