如何在WordPress前端实现自定义分类法翻译(无需插件方案)
可行方案:无插件实现WordPress前端自定义分类法翻译
你的思路完全靠谱——不需要任何插件,直接通过主题的functions.php拦截自定义分类法的渲染流程,用本地存储的翻译字符串替换原内容就行。下面我给你分享几个实用的实现方式,都是纯代码搞定:
方法一:用PHP数组直接存储翻译(最简单)
这种方式适合翻译内容不多的场景,直接在代码里定义翻译映射,通过WordPress的过滤器钩子拦截输出:
// 在主题的functions.php中添加以下代码 function custom_taxonomy_translate($term_name, $term_id) { // 定义翻译映射:键是原分类名称,值是翻译后的名称 $translations = [ 'Electronics' => '电子产品', 'Clothing' => '服装', 'Books' => '图书' // 按需添加更多翻译条目 ]; // 检查当前分类名是否有对应翻译,有则返回翻译值,否则返回原名称 return isset($translations[$term_name]) ? $translations[$term_name] : $term_name; } // 拦截分类归档页面的标题输出 add_filter('single_term_title', 'custom_taxonomy_translate', 10, 2); // 拦截文章页面中分类列表的输出(比如用the_terms()函数渲染的情况) function custom_term_list_translate($term_list, $terms, $taxonomy, $args) { // 只处理你的自定义分类法,替换成实际的分类法slug if ($taxonomy !== 'product_category') { return $term_list; } $translations = [ 'Electronics' => '电子产品', 'Clothing' => '服装', 'Books' => '图书' ]; $new_term_list = ''; foreach ($terms as $term) { $translated_name = isset($translations[$term->name]) ? $translations[$term->name] : $term->name; $new_term_list .= '<a href="' . get_term_link($term) . '">' . $translated_name . '</a>, '; } // 移除最后多余的逗号和空格 return rtrim($new_term_list, ', '); } add_filter('the_terms', 'custom_term_list_translate', 10, 4);
方法二:用JSON文件存储翻译(便于管理)
如果翻译内容较多,或者需要非技术人员维护,把翻译存在JSON文件里更方便,不用修改代码就能更新翻译:
- 在主题根目录创建
taxonomy-translations.json文件,内容示例:
{ "Electronics": "电子产品", "Clothing": "服装", "Books": "图书" }
- 在
functions.php中添加读取JSON并拦截输出的代码:
// 读取JSON翻译文件的辅助函数 function get_taxonomy_translations() { $json_file_path = get_stylesheet_directory() . '/taxonomy-translations.json'; // 如果文件不存在,返回空数组 if (!file_exists($json_file_path)) { return []; } // 读取并解析JSON内容 $json_content = file_get_contents($json_file_path); return json_decode($json_content, true) ?: []; } // 拦截分类归档标题 function custom_taxonomy_translate_from_json($term_name, $term_id) { $translations = get_taxonomy_translations(); return isset($translations[$term_name]) ? $translations[$term_name] : $term_name; } add_filter('single_term_title', 'custom_taxonomy_translate_from_json', 10, 2); // 拦截文章页面分类列表 function custom_term_list_translate_from_json($term_list, $terms, $taxonomy, $args) { if ($taxonomy !== 'product_category') { return $term_list; } $translations = get_taxonomy_translations(); $new_term_list = ''; foreach ($terms as $term) { $translated_name = isset($translations[$term->name]) ? $translations[$term->name] : $term->name; $new_term_list .= '<a href="' . get_term_link($term) . '">' . $translated_name . '</a>, '; } return rtrim($new_term_list, ', '); } add_filter('the_terms', 'custom_term_list_translate_from_json', 10, 4);
额外技巧与注意事项
- 指定目标分类法:记得把代码中的
product_category替换成你实际的自定义分类法slug,避免影响默认分类、标签等其他分类法。 - 多语言适配:如果需要根据用户语言切换翻译,可以结合
get_locale()函数加载对应语言的JSON文件,比如taxonomy-translations-zh_CN.json、taxonomy-translations-en_US.json。 - 自定义模板直接修改:如果你用了自定义的分类归档模板(比如
taxonomy-{taxonomy}.php)或者文章模板,也可以直接在模板里手动替换分类名称,不需要用过滤器,示例:
$terms = get_the_terms(get_the_ID(), 'product_category'); if ($terms) { $translations = get_taxonomy_translations(); foreach ($terms as $term) { $translated_name = isset($translations[$term->name]) ? $translations[$term->name] : $term->name; echo '<a href="' . get_term_link($term) . '">' . $translated_name . '</a>'; } }
- 性能优化:如果翻译内容很多,建议用
wp_cache_set缓存读取的JSON内容,避免每次请求都读取文件,提升页面加载速度。
内容的提问来源于stack exchange,提问作者Badr Louachama
相关产品推荐
相关产品推荐

