WPML 是用于管理多语言网站的优秀插件,因为它允许编辑与翻译内容相关的大量相关信息。然而,虽然这个插件添加了 hreflang 标签,但仍然缺乏对 hreflang="x-default" 注释的支持。
所以我想知道如何将这个 x-default 标签添加到 WPML 网站,并使其指向我想要的 URL 语言版本。
WPML 论坛中有一些代码示例,但它们适用于该插件的旧版本。它们都指向编辑 head_langs 函数,该函数当前包含以下代码:
function head_langs()
{
$languages = $this->get_ls_languages( array( 'skip_missing' => true ) );
// If there are translations and is not paged content...
//Renders head alternate links only on certain conditions
$the_post = get_post();
$the_id = $the_post ? $the_post->ID : false;
$is_valid = count( $languages ) > 1 && !is_paged() && ( ( ( is_single() || is_page() ) && $the_id && get_post_status( $the_id ) == 'publish' ) || ( is_home() || is_front_page() || is_archive() ) );
if ( $is_valid ) {
foreach ( $languages as $code => $lang ) {
$alternate_hreflang = apply_filters( 'wpml_alternate_hreflang', $lang[ 'url' ], $code );
printf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL,
$this->get_language_tag( $code ),
str_replace( '&', '&', $alternate_hreflang ) );
}
}
}