OneNav主题修改日志-1:页脚设置 添加多语言功能
项目名称:OneNav WordPress主题
修改摘要
- ✅ 为”自定义页脚版权”添加多语言功能
- ✅ 为”板块一(更多内容)”添加多语言功能
- ✅ 为”板块二”添加多语言功能(包括内容和菜单)
- ✅ 为”板块三(页脚图片显示文字)”添加多语言功能
- ✅ 修复多语言菜单显示问题
- ❌ 移除了”下载页版权声明”的多语言功能(因为涉及到在 functions.php 中修改代码,暂时不修改)
一、功能概述
本次修改为OneNav主题的页脚设置模块添加了完整的多语言支持功能。所有多语言字段均采用统一的group类型配置,每个语言版本包含语言缩写和内容字段,支持根据不同语言自动显示对应的内容。
注意:使用多语言功能前,需要在”基础设置”中开启”多语言”选项(m_language)。
二、修改的文件列表
| 文件路径 | 修改内容 | 修改类型 |
|---|---|---|
inc/configs/theme/basic-footer.php |
添加多语言字段配置 | 新增配置 |
inc/functions/io-footer.php |
修改输出函数支持多语言 | 功能增强 |
三、自定义页脚版权多语言功能
3.1 配置文件修改
文件:inc/configs/theme/basic-footer.php
添加了 footer_copyright_multi 多语言字段,保留 footer_copyright 作为单语言版本(向后兼容)。
array(
'id' => 'footer_copyright_multi',
'type' => 'group',
'title' => '自定义页脚版权(多语言)',
'fields' => array(
array(
'id' => 'language',
'type' => 'text',
'title' => '语言缩写',
'after' => '如:zh en ,各国语言缩写参考',
'class' => 'compact min',
),
array(
'id' => 'content',
'type' => 'wp_editor',
'title' => '内容',
'desc' => '支持HTML代码,请注意代码规范及标签闭合',
'height' => '100px',
'sanitize' => false,
'class' => 'compact min',
),
),
'before' => '需在基础设置开启多语言(默认语言放第一个)',
'class' => 'compact min',
'button_title' => '添加语言',
'accordion_title_prefix' => '语言:',
),
3.2 输出函数修改
文件:inc/functions/io-footer.php
修改 io_copyright() 函数,优先使用多语言版本。
function io_copyright($class = '', $simple = false, $echo = true) {
$html = '';
$aff = io_get_option('io_aff', true) ? '由 OneNav 强力驱动 ' : '';
// 优先使用多语言版本的页脚版权(io_get_option会自动处理多语言)
$footer_copyright_multi = io_get_option('footer_copyright_multi', '');
// 如果多语言版本为空,使用单语言版本(向后兼容)
$footer_copyright = !empty($footer_copyright_multi) ? $footer_copyright_multi : io_get_option('footer_copyright', '');
if ($footer_copyright && !$simple) {
$html .= $footer_copyright . " " . $aff . io_get_option('footer_statistics', '');
} else {
// ... 原有逻辑
}
// ...
}
四、板块一(更多内容)多语言功能
4.1 配置添加
array(
'id' => 'footer_t1_code_multi',
'type' => 'group',
'title' => ' ',
'subtitle' => '更多内容(多语言)',
'fields' => array(
array(
'id' => 'language',
'type' => 'text',
'title' => '语言缩写',
),
array(
'id' => 'content',
'type' => 'textarea',
'title' => '内容',
'sanitize' => false,
'attributes' => array('rows' => 3),
),
),
),
4.2 输出函数修改
// 优先使用多语言版本的板块一内容
$footer_t1_code_multi = io_get_option('footer_t1_code_multi', '');
$footer_t1_code = !empty($footer_t1_code_multi) ? $footer_t1_code_multi : io_get_option('footer_t1_code', '');
$footer_t1 .= '' . $footer_t1_code . '';
五、板块二多语言功能(含菜单支持)
重要:板块二支持同时设置内容和菜单的多语言版本,菜单优先级高于内容。
5.1 配置添加
array(
'id' => 'footer_t2_code_multi',
'type' => 'group',
'title' => '板块二(多语言)',
'subtitle' => '建议为友情链接,或者站内链接',
'fields' => array(
array(
'id' => 'language',
'type' => 'text',
'title' => '语言缩写',
),
array(
'id' => 'content',
'type' => 'textarea',
'title' => '内容',
),
array(
'id' => 'nav',
'type' => 'select',
'title' => '请选择菜单',
'options' => 'menus',
'after' => '为空则显示上面"内容"字段',
),
),
),
5.2 输出函数修改(关键修复)
重要修复:由于
io_get_option 会对包含 multi 的字段自动处理,导致无法获取完整数组。解决方案是直接从数据库获取原始数据。$footer_t2 = '';
// 直接从数据库获取原始数据,跳过_iol函数的自动处理
$options = get_option('io_get_option');
$footer_t2_code_multi = isset($options['footer_t2_code_multi']) ? $options['footer_t2_code_multi'] : '';
$footer_t2_nav = io_get_option('footer_t2_nav', '');
if (!empty($footer_t2_code_multi) && is_array($footer_t2_code_multi)) {
// 使用多语言版本
$current_lang = determine_locale();
$lang_code = explode('_', $current_lang)[0];
$found_content = '';
$found_nav = '';
$found = false;
// 查找当前语言对应的内容
foreach ($footer_t2_code_multi as $item) {
if (isset($item['language'])) {
$item_lang = $item['language'];
if ($item_lang == $current_lang || $item_lang == $lang_code) {
$found_content = isset($item['content']) ? $item['content'] : '';
$found_nav = isset($item['nav']) ? $item['nav'] : '';
$found = true;
break;
}
}
}
// 如果没有找到匹配的语言,使用第一个作为默认
if (!$found && !empty($footer_t2_code_multi[0])) {
$found_content = isset($footer_t2_code_multi[0]['content']) ? $footer_t2_code_multi[0]['content'] : '';
$found_nav = isset($footer_t2_code_multi[0]['nav']) ? $footer_t2_code_multi[0]['nav'] : '';
}
// 优先显示菜单(如果有)
if ($found_nav) {
$footer_t2 .= '
';
$menu_output = wp_nav_menu(array(
'menu' => $found_nav,
'container' => false,
'items_wrap' => '%3$s',
'echo' => false
));
$footer_t2 .= $menu_output ? $menu_output : '';
$footer_t2 .= '
';
}
// 如果没有菜单,显示内容(如果有)
elseif ($found_content) {
$footer_t2 .= '
' . $found_content . '
';
}
} else {
// 使用单语言版本(向后兼容)
// ...
}
六、板块三(页脚图片显示文字)多语言功能
6.1 配置添加
在 footer_t3_img group 字段中添加了 text_multi 子字段:
array(
'id' => 'footer_t3_img',
'type' => 'group',
'fields' => array(
array(
'id' => 'text_multi',
'type' => 'group',
'title' => '显示文字(多语言)',
'fields' => array(
array(
'id' => 'language',
'type' => 'text',
'title' => '语言缩写',
),
array(
'id' => 'content',
'type' => 'text',
'title' => '内容',
),
),
),
array(
'id' => 'text',
'type' => 'text',
'title' => '显示文字(单语言)',
'dependency' => array('text_multi', '==', ''),
),
// ...
),
),
6.2 输出函数修改
foreach ($f_imgs as $f_img) {
// 优先使用多语言版本的显示文字
$f_img_text = '';
if (!empty($f_img['text_multi']) && is_array($f_img['text_multi'])) {
// 使用_iol函数处理多语言text_multi字段
$f_img_text = _iol($f_img['text_multi'], 'text_multi');
}
// 如果text_multi为空或处理失败,使用单语言版本的text
if (empty($f_img_text) && isset($f_img['text'])) {
$f_img_text = $f_img['text'];
}
// ... 输出HTML
}
七、问题修复记录
7.1 板块二菜单显示问题
问题描述
板块二多语言设置的菜单无法显示,只能显示单语言中选择的菜单。
根本原因
io_get_option('footer_t2_code_multi', '') 会通过 _iol() 函数自动处理。对于包含 multi 的字段,_iol() 会返回当前语言对应的字符串(只返回 content 字段),而不是完整的数组结构,导致无法访问 nav 字段。
解决方案
使用 get_option('io_get_option') 直接从数据库获取原始数据,跳过 _iol 函数的自动处理,确保能够获取完整的多语言数组结构。
修复结果:✅ 问题已解决,多语言菜单现在可以正常显示。
八、使用方法
8.1 启用多语言功能
- 进入 WordPress 后台 → 外观 → 主题设置
- 在 基础设置 中找到 多语言 选项
- 开启多语言开关
- 填写 语言列表(例如:en|ja,默认语言不需要填)
- 保存设置
提示:建议配合使用 Polylang 等 WordPress 多语言插件。
8.2 配置多语言内容
- 进入 页脚设置
- 找到需要设置多语言的字段(如”自定义页脚版权(多语言)”)
- 点击 添加语言 按钮
- 填写语言缩写(如:zh、en)和对应内容
- 重复步骤3-4,添加更多语言版本
- 保存设置
8.3 板块二菜单配置(特殊说明)
- 每个语言版本可以单独选择对应的菜单
- 如果同时填写了”内容”和”菜单”,菜单会优先显示
- 如果菜单为空,则显示”内容”字段
- 建议配合多语言插件(如 Polylang)创建不同语言的菜单
九、技术说明
9.1 多语言处理机制
主题使用了 _iol() 函数来自动处理多语言内容:
- 字段名包含 “multi”:自动识别为多语言数组类型
- 字符串格式:支持
zh=*=中文|*|en=*=English格式 - 自动语言匹配:根据
determine_locale()返回的当前语言自动匹配
9.2 向后兼容性
所有多语言字段都保留了对应的单语言版本,确保:
- 未开启多语言时,仍可使用单语言版本
- 已配置的站点无需重新配置即可继续使用
- 多语言版本为空时,自动回退到单语言版本
注意事项:
- 默认语言建议放在多语言配置的第一位
- 语言缩写使用 ISO 639-1 标准(2个字母,如:zh、en)
- 设置多语言后,需重新保存一次”固定链接”设置
十、文件修改清单
| 文件 | 修改内容 | 行数(约) |
|---|---|---|
inc/configs/theme/basic-footer.php |
添加多个多语言group字段配置 | ~150行新增 |
inc/functions/io-footer.php |
修改io_copyright()和io_big_footer()函数 | ~100行修改 |
inc/functions/functions.php |
恢复down_statement单语言版本 | ~3行修改 |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...