惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
P
Palo Alto Networks Blog
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
aimingoo的专栏
aimingoo的专栏
腾讯CDC
T
Threatpost
D
DataBreaches.Net
Vercel News
Vercel News
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
U
Unit 42
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
O
OpenAI News
量子位
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Visual Studio Blog
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
S
Security Affairs
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
S
SegmentFault 最新的问题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
I
Intezer
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
博客园_首页
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

模板兔

WordPress模板开发之hook钩子media_view_strings的使用教程 WordPress插件开发之manage_comments_custom_column的使用方法 WordPress主题定制之hook钩子media_upload_tabs的用法详解 WordPress插件开发之lostpassword_user_data钩子的使用教程 WordPress模板开发之manage_media_columns钩子的用法详解 WordPress主题开发之manage_media_custom_column钩子的使用教程 WordPress模板开发之hook钩子pre_get_col_charset的用法 - 模板兔 WordPress主题开发之pre_months_dropdown_query的用法说明 - 模板兔 WordPress主题开发之hook钩子pre_load_script_translations的使用教程 - 模板兔 WordPress插件开发之hook钩子network_plugin_loaded的使用说明 - 模板兔 WordPress插件开发之pre_render_block的使用教程 - 模板兔 WordPress hook钩子pre_oembed_result的用法详解 - 模板兔 wordpress前台某处上传图片到媒体库单独放进一个文件夹_WordPress教程 - 模板兔 WordPress主题定制之hook钩子post_locked_dialog的用法详解 - 模板兔 WordPress插件开发之hook钩子pre_cache_alloptions的用法详解 - 模板兔 WordPress模板定制开发之previous_comments_link_attributes的调用方法 - 模板兔 WordPress主题开发之hook钩子post_comments_feed_link_html的用法介绍 - 模板兔 WordPress模板开发之hook钩子posts_clauses的使用教程解析 - 模板兔 WordPress插件开发之hook钩子mejs_settings的用法详解 - 模板兔 WordPress主题定制之post_playlist钩子的使用教程 - 模板兔 WordPress模板开发之hook钩子post_embed_url的用法详解 - 模板兔 WordPress模板开发之preprocess_comment钩子的用法介绍 - 模板兔 WordPress插件开发之hook钩子populate_site_meta的用法详解 - 模板兔 WordPress主题开发之posts_request_ids钩子的用法详解 - 模板兔 WordPress模板定制开发之pre_category_nicename的用法介绍 - 模板兔
wordpress获取指定多个分类ID以及其子分类_WordPress教程 - 模板兔
模板兔 · 2026-05-24 · via 模板兔

wp_dropdown_categories 原生不支持直接传入多个父分类 ID,但可以通过 自定义查询 + 拼接 HTML 完美实现:指定多个顶级分类,自动展示它们的所有子分类(层级缩进)。

直接把这段代码放到你的主题 functions.php 或自定义插件中:

function custom_dropdown_multiple_categories($parent_ids = [], $args = []) {
if (empty($parent_ids)) return '';

$all_items = [];

// 遍历每个指定父分类,把【父分类本身 + 全部子分类】都加入列表
foreach ($parent_ids as $parent_id) {
// 1. 先加入父分类本身
$parent_cat = get_category($parent_id);
if ($parent_cat && !is_wp_error($parent_cat)) {
$all_items[] = $parent_cat;
}

// 2. 再获取该父分类下所有层级子分类
$child_cats = get_categories([
'parent' => $parent_id,//仅显示二级,child_of就是所有子分类
'hide_empty' => false,
'hierarchical' => true,
]);

$all_items = array_merge($all_items, $child_cats);
}

// 去重
$term_ids = array_unique(wp_list_pluck($all_items, 'term_id'));
$unique_items = [];
foreach ($term_ids as $id) {
foreach ($all_items as $item) {
if ($item->term_id == $id) {
$unique_items[] = $item;
break;
}
}
}
$all_items = $unique_items;

// 默认参数
$default_args = [
'show_option_none' => '选择分类',
'option_none_value' => '',
'name' => 'cat',
'id' => 'cat',
'selected' => 0,
];
$args = wp_parse_args($args, $default_args);

// 拼接下拉菜单
$html = '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['id']) . '">';
$html .= '<option value="' . esc_attr($args['option_none_value']) . '">' . esc_html($args['show_option_none']) . '</option>';

foreach ($all_items as $cat) {
$depth = count(get_ancestors($cat->term_id, 'category'));
$indent = str_repeat('&nbsp;&nbsp;', $depth);
//$selected = selected($args['selected'], $cat->term_id, false);

$html .= '<option value="' . esc_attr($cat->term_id) . '">';
$html .= $indent . esc_html($cat->name);
$html .= '</option>';
}

$html .= '</select>';
return $html;
}

// 你要显示的多个父分类ID 

$parent_ids = [2,5,8]; 

// 输出下拉菜单 

echo custom_dropdown_multiple_categories($parent_ids);