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

WordPress自定义分类法genre层级展示格式错误修复求助

修复方案及代码实现

关键问题分析

你的代码存在三个核心问题:

  1. 父分类分隔符错误(用, 而非预期的| )
  2. 孙分类分隔符错误(用, 而非预期的/)
  3. 层级处理逻辑导致重复输出父分类名称,且HTML实体转义错误导致标签解析异常

修复后的代码实现

1. 更新functions.php中的函数

function get_taxonomy_output($terms) {
    if (is_wp_error($terms) || empty($terms)) {
        return '';
    }

    // 整理术语层级结构:key为父term_id,value为子术语数组
    $term_hierarchy = array();
    // 收集所有顶层父术语(parent=0)
    $parent_terms = array();

    foreach ($terms as $term) {
        $term_hierarchy[$term->parent][] = $term;
        if ($term->parent === 0) {
            $parent_terms[$term->term_id] = $term;
        }
    }

    // 确保关联子分类时,顶层父分类不会被遗漏
    foreach ($terms as $term) {
        if ($term->parent !== 0) {
            $top_parent = get_term_by('id', $term->parent, 'genre');
            while ($top_parent && $top_parent->parent !== 0) {
                $top_parent = get_term_by('id', $top_parent->parent, 'genre');
            }
            if ($top_parent) {
                $parent_terms[$top_parent->term_id] = $top_parent;
            }
        }
    }

    $output = array();
    foreach ($parent_terms as $parent_term) {
        $output[] = build_genre_term_output($parent_term, $term_hierarchy);
    }

    // 父分类之间用 | 分隔
    return implode(' | ', $output);
}

function build_genre_term_output($term, $term_hierarchy) {
    // 生成符合安全规范的术语链接
    $term_link = sprintf(
        '<a href="%s">%s</a>',
        esc_url(get_term_link($term)),
        esc_html($term->name)
    );

    // 处理当前术语的子分类
    if (!empty($term_hierarchy[$term->term_id])) {
        $child_output = array();
        foreach ($term_hierarchy[$term->term_id] as $child) {
            // 处理子分类的孙分类
            if (!empty($term_hierarchy[$child->term_id])) {
                $grandchild_links = array();
                foreach ($term_hierarchy[$child->term_id] as $grandchild) {
                    $grandchild_links[] = sprintf(
                        '<a href="%s">%s</a>',
                        esc_url(get_term_link($grandchild)),
                        esc_html($grandchild->name)
                    );
                }
                // 孙分类用 / 分隔
                $child_output[] = sprintf(
                    '<a href="%s">%s</a> (%s)',
                    esc_url(get_term_link($child)),
                    esc_html($child->name),
                    implode('/', $grandchild_links)
                );
            } else {
                // 普通子分类直接生成链接
                $child_output[] = sprintf(
                    '<a href="%s">%s</a>',
                    esc_url(get_term_link($child)),
                    esc_html($child->name)
                );
            }
        }
        // 子分类用 , 分隔
        $term_link .= ' (' . implode(', ', $child_output) . ')';
    }

    return $term_link;
}

2. single.php中的调用保持不变

$categories = get_the_terms(get_the_ID(), 'genre');
$output_genre = get_taxonomy_output($categories);
// 输出到页面
echo $output_genre;

修复说明

  • 分隔符匹配预期:父分类用| 分隔,子分类用, 分隔,孙分类用/分隔,完全符合你要求的格式
  • 层级逻辑优化:自动补全顶层父分类,避免关联子分类时遗漏父分类展示,同时消除重复输出问题
  • 符合WordPress规范:使用esc_url()和esc_html()做安全转义,遵循官方开发标准
  • HTML输出修复:直接生成正确的<a>标签,不再使用错误的HTML实体转义,确保浏览器正常解析

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:14:52