这是我在使用qTranslate时遇到的一个问题,但我认为解决方案更多的是指导Wordpress挂钩和过滤器如何工作。
我没有使用qTranslate小部件,而是使用了钩子
<?php echo qtrans_generateLanguageSelectCode(\'text\');?>
调用标题中的语言选择器,然后修改其在CSS中的外观。这没问题。但是,我想在语言选择代码函数中添加一个“样式”,这样就不会显示完整的语言文本(在我的例子中是“法语”和“英语”),而是只显示语言代码(
EN
和
FR
). 我通过改变发现了这一点(我知道这是一件非常糟糕的事情)
qtranslate_widget.php
并为功能添加新案例
qtrans_generateLanguageSelectCode($style=\'\', $id=\'\')
.
这很有效,但后来我更新了Wordpress,还必须更新qTranslate,所以很明显我的修改消失了。因此,我不想重新执行该操作,而是想知道是否有人对如何在我自己的主题函数中向函数添加过滤器有任何建议。php?我想应该是这样的
add_filter(\'qtrans_generateLanguageSelectCode\', \'qtrans_myLanguageSelectcode\',
但在那之后,我迷路了,对过滤器的了解不够。我希望这足够清楚!
当我更新时,我显然丢失了我的修改,但我认为下面应该是可行的(我还没有测试!)
case \'code\':
foreach(qtrans_getSortedLanguages() as $language) {
$classes = array(\'lang-\'.$language);
if($language == $q_config[\'language\'])
$classes[] = \'active\';
echo \'<a href="\'.qtrans_convertURL($url, $language).\'" class="\'. implode(\' \', $classes) .\'"\';
// set hreflang
echo \'hreflang="\'.$language.\'" title="\'.$q_config[\'language_name\'][$language].\'"\';
echo \'>\'\'.$language.\'</a>\';
}
break;
不要让这篇文章太长,但以下是qTranslate插件中现有的函数:
// Language Select Code for non-Widget users
function qtrans_generateLanguageSelectCode($style=\'\', $id=\'\') {
global $q_config;
if($style==\'\') $style=\'text\';
if(is_bool($style)&&$style) $style=\'image\';
if(is_404()) $url = get_option(\'home\'); else $url = \'\';
if($id==\'\') $id = \'qtranslate\';
$id .= \'-chooser\';
switch($style) {
case \'image\':
case \'text\':
case \'dropdown\':
echo \'<ul class="qtrans_language_chooser" id="\'.$id.\'">\';
foreach(qtrans_getSortedLanguages() as $language) {
$classes = array(\'lang-\'.$language);
if($language == $q_config[\'language\'])
$classes[] = \'active\';
echo \'<li class="\'. implode(\' \', $classes) .\'"><a href="\'.qtrans_convertURL($url, $language).\'"\';
// set hreflang
echo \' hreflang="\'.$language.\'" title="\'.$q_config[\'language_name\'][$language].\'"\';
if($style==\'image\')
echo \' class="qtrans_flag qtrans_flag_\'.$language.\'"\';
echo \'><span\';
if($style==\'image\')
echo \' style="display:none"\';
echo \'>\'.$q_config[\'language_name\'][$language].\'</span></a></li>\';
}
echo "</ul><div class=\\"qtrans_widget_end\\"></div>";
if($style==\'dropdown\') {
echo "<script type=\\"text/javascript\\">\\n// <![CDATA[\\r\\n";
echo "var lc = document.getElementById(\'".$id."\');\\n";
echo "var s = document.createElement(\'select\');\\n";
echo "s.id = \'qtrans_select_".$id."\';\\n";
echo "lc.parentNode.insertBefore(s,lc);";
// create dropdown fields for each language
foreach(qtrans_getSortedLanguages() as $language) {
echo qtrans_insertDropDownElement($language, qtrans_convertURL($url, $language), $id);
}
// hide html language chooser text
echo "s.onchange = function() { document.location.href = this.value;}\\n";
echo "lc.style.display=\'none\';\\n";
echo "// ]]>\\n</script>\\n";
}
break;
case \'both\':
echo \'<ul class="qtrans_language_chooser" id="\'.$id.\'">\';
foreach(qtrans_getSortedLanguages() as $language) {
echo \'<li\';
if($language == $q_config[\'language\'])
echo \' class="active"\';
echo \'><a href="\'.qtrans_convertURL($url, $language).\'"\';
echo \' class="qtrans_flag_\'.$language.\' qtrans_flag_and_text" title="\'.$q_config[\'language_name\'][$language].\'"\';
echo \'><span>\'.$q_config[\'language_name\'][$language].\'</span></a></li>\';
}
echo "</ul><div class=\\"qtrans_widget_end\\"></div>";
break;
}
}
最合适的回答,由SO网友:jessica_b 整理而成
在我的header.php
, 我想让qTranslate语言选择器住在哪里,我把它放在:
<?php echo qtrans_SelectCode(\'code\');?>
然后,我将此代码添加到
functions.php
. 这有点多余,因为它重复了内置的qTranslate选项(图像、文本、下拉列表),我没有在页面上使用这些选项,但我想保留代码,因为我的过滤器正在覆盖
qtrans_generateLanguageSelectCode
作用(如果有人能想出如何只添加一个样式而不覆盖整个函数,那就太棒了!)
我使用的样式称为“代码”(您将在底部附近看到),只输出语言代码-即:FR
和EN
就我而言。通过创建新样式,您可以编写任何想要的输出:
if( function_exists( \'qtrans_getLanguage\' ) )
{
// qTranslate Language Select Code filter
add_filter( \'qtrans_generateLanguageSelectCode\', \'qtrans_SelectCode\' );
function qtrans_SelectCode( $style = \'\', $id = \'\' ) {
global $q_config;
if( $style == \'\' )
$style = \'text\';
if( is_bool( $style ) && $style )
$style = \'image\';
if( is_404() )
$url = get_option( \'home\' );
else
$url = \'\';
if( $id == \'\' )
$id = \'qtranslate\';
$id .= \'-chooser\';
switch( $style ) {
case \'image\':
case \'text\':
case \'dropdown\':
echo \'<ul class="qtrans_language_chooser" id="\' . $id . \'">\';
foreach( qtrans_getSortedLanguages() as $language ) {
$classes = array( \'lang-\' . $language );
if( $language == $q_config[\'language\'] )
$classes[] = \'active\';
echo \'<li class="\' . implode( \' \', $classes ) . \'"><a href="\' . qtrans_convertURL( $url, $language ) . \'"\';
// set hreflang
echo \' hreflang="\' . $language . \'" title="\' . $q_config[\'language_name\'][$language] . \'"\';
if( $style == \'image\' )
echo \' class="qtrans_flag qtrans_flag_\' . $language . \'"\';
echo \'><span\';
if( $style == \'image\' )
echo \' style="display:none"\';
echo \'>\' . $q_config[\'language_name\'][$language] . \'</span></a></li>\';
}
echo "</ul><div class=\\"qtrans_widget_end\\"></div>";
if( $style == \'dropdown\' ) {
echo "<script type=\\"text/javascript\\">\\n// <![CDATA[\\r\\n";
echo "var lc = document.getElementById(\'" . $id . "\');\\n";
echo "var s = document.createElement(\'select\');\\n";
echo "s.id = \'qtrans_select_" . $id . "\';\\n";
echo "lc.parentNode.insertBefore(s,lc);";
// create dropdown fields for each language
foreach( qtrans_getSortedLanguages() as $language ) {
echo qtrans_insertDropDownElement( $language, qtrans_convertURL( $url, $language ), $id );
}
// hide html language chooser text
echo "s.onchange = function() { document.location.href = this.value;}\\n";
echo "lc.style.display=\'none\';\\n";
echo "// ]]>\\n</script>\\n";
}
break;
case \'both\':
echo \'<ul class="qtrans_language_chooser" id="\' . $id . \'">\';
foreach( qtrans_getSortedLanguages() as $language ) {
echo \'<li\';
if( $language == $q_config[\'language\'] )
echo \' class="active"\';
echo \'><a href="\' . qtrans_convertURL( $url, $language ) . \'"\';
echo \' class="qtrans_flag_\' . $language . \' qtrans_flag_and_text" title="\' . $q_config[\'language_name\'][$language] . \'"\';
echo \'><span>\' . $q_config[\'language_name\'][$language] . \'</span></a></li>\';
}
echo "</ul><div class=\\"qtrans_widget_end\\"></div>";
break;
case \'code\':
$count = 0;
foreach( qtrans_getSortedLanguages() as $language ) {
if( $count > 0 )
print \' \';
$count++;
if( $language == $q_config[\'language\'] ) {
print \'<span class="qtrans_language_chooser active" title="\' . $q_config[\'language_name\'][$language] . \'">\';
print $language;
print \'</span>\';
}
else {
print \'<a href="\' . qtrans_convertURL( $url, $language ) . \'" class="qtrans_language_chooser"\';
print \' hreflang="\' . $language . \'" title="\' . $q_config[\'language_name\'][$language] . \'">\';
print $language;
print \'</a>\';
}
}
break;
}
}
}
重要的是将函数包含在:
if(function_exists(\'qtrans_getLanguage\'))
如果qTranslate插件损坏或您停用它,否则
functions.php
将无法正常工作,您将无法访问您的站点!
很抱歉,我花了一点时间才回来,我不是一个专业的web开发人员,所以我已经几个月没有接触过这些东西了。我完全自学成才,这就是为什么我的术语可能有点不清楚的原因。但我希望这有帮助!