将基于本地语言的 WordPress 标头代码添加到 functions.php 中 [英] Adding a code to WordPress header based on local language into functions.php
问题描述
我必须根据网站使用的当前语言向 WordPress 标头添加 Javascript 代码.我们使用 WPML 字符串翻译.我想写一个这样的函数:
I have to add a Javascript code to a WordPress header based on the current language used by the website. We use WPML string translation. I want to write a function like this:
function add_customcode_header(){
?>
if(ICL_LANGUAGE_CODE == 'ru')
{
<script>
code 1
</script>
}
else
{
<script>
code 2
</script>
}
}
<?php
add_action('wp_head', 'add_customcode_header');
如果网站语言参数设置为'ru',并且如果语言参数是任何其他参数,则该函数应该将带有code 1
内容的脚本添加到code 2
其他语言
This function should place the script with code 1
contents if the website language parameter is set to 'ru' and IF the language parameter is any other, it should add the code 2
for other languages
神奇的问题是——这个功能能正常工作吗?
The magic question is - will this function work properly?
更新:
有效的解决方案:
function add_customcode_header(){
if(ICL_LANGUAGE_CODE == 'ru')
{
echo '<script>
code 1
</script>';
}
else
{
echo '<script>
code 2
</script>';
}
}
add_action('wp_head', 'add_customcode_header');
推荐答案
您的整体想法应该没问题,但是,在您发布的示例中,存在一些语法错误.
Your overall idea should work fine, however, in the example you posted, there were some syntax errors.
function add_customcode_header(){
if(ICL_LANGUAGE_CODE == 'ru')
{
echo '<script>
code 1
</script>';
}
else
{
echo '<script>
code 2
</script>';
}
}
add_action('wp_head', 'add_customcode_header');
或
function add_customcode_header(){
if(ICL_LANGUAGE_CODE == 'ru')?>
<script>
code 1
</script>
<?php
else ?>
<script>
code 2
</script>
<?php
}
add_action('wp_head', 'add_customcode_header');
两者在语法上都应该是正确的,这真的很重要,你更喜欢什么.
Should both be syntactically correct, and it really just matters what you're more comfortable with.
这篇关于将基于本地语言的 WordPress 标头代码添加到 functions.php 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!