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

求助:用Astra主题与Polylang实现免费多语言页脚配置

解决Astra免费版+Polylang的多语言页脚方案

以下是几个无需购买Astra专业版、适配Polylang的可行方案,代码简单易操作:


方案1:小工具+语言条件隐藏(最适合新手)

利用Astra的页脚小工具区域,配合Polylang的语言判断实现内容切换:

  1. 进入外观→小工具,给页脚区域添加2个「文本」小工具:一个输入英文页脚内容,一个输入西班牙语内容。
  2. 右键点击页脚区域→「检查元素」,找到两个小工具的ID(比如text-2、text-3)。
  3. 将以下代码添加到子主题的functions.php中(必须用子主题,避免主题更新丢失代码):
// 根据当前语言显示对应页脚小工具
add_action('wp_footer', 'astra_polylang_footer_switch', 10);
function astra_polylang_footer_switch() {
    $current_lang = pll_current_language(); // 获取当前Polylang语言代码
    
    // 替换为你的小工具ID和语言代码(en=英文,es=西班牙语)
    if ($current_lang == 'en') {
        echo '<style>#text-3 { display: none; }</style>'; // 隐藏西语小工具
    } elseif ($current_lang == 'es') {
        echo '<style>#text-2 { display: none; }</style>'; // 隐藏英文小工具
    }
}
  1. 保存后刷新页面,切换语言即可看到对应页脚内容。

方案2:直接修改页脚模板(适合需要自定义HTML结构的场景)

  1. 复制Astra主题目录下的 inc/templates/parts/footer.php 文件,粘贴到子主题的对应目录(没有文件夹就新建)。
  2. 打开子主题的footer.php,找到页脚内容的输出位置,替换为以下代码:
// 输出多语言页脚内容
if (pll_current_language() == 'en') {
    echo '<div class="ast-footer-content">© 2024 Your Site Name. All Rights Reserved. | Contact: info@yoursite.com</div>';
} elseif (pll_current_language() == 'es') {
    echo '<div class="ast-footer-content">© 2024 Tu Nombre de Sitio. Todos los Derechos Reservados. | Contacto: info@tusitio.com</div>';
}
  1. 保存文件,切换语言即可生效。

方案3:Polylang字符串翻译(最规范的多语言管理方式)

如果页脚是多个固定字符串,可将其注册为Polylang可翻译项,方便后台管理:

  1. 在子主题functions.php中添加注册代码:
// 注册页脚可翻译字符串
add_action('init', 'register_footer_translatable_strings');
function register_footer_translatable_strings() {
    pll_register_string('footer-copyright', '© 2024 Your Site Name. All Rights Reserved', 'Footer');
    pll_register_string('footer-contact', 'Contact Us: info@yoursite.com', 'Footer');
}
  1. 进入Polylang→字符串翻译,找到刚注册的「Footer」分组,将字符串翻译成西班牙语。
  2. 在页脚模板或小工具中调用翻译后的内容:
echo pll__('footer-copyright');
echo '<br>';
echo pll__('footer-contact');

注意事项

  • 所有代码必须添加到子主题的functions.php,不要直接修改Astra父主题文件。
  • Polylang的语言代码需与后台设置一致(可在「Polylang→语言」中查看,英文默认en,西语默认es)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 23:55:19