WPML
状态
已解决
解决于
1.2.0
相关插件/主题
Yoast SEO

问题概述

在 Yoast 14.x 进行近期重大更改后,部分 WPML 用户发现 Yoast 内容始终以默认语言生成。

例如,在次要语言页面上,这可能是“meta”标签(如“og:title”或“og:description”),或者是面包屑导航中的 URL。

我们提供了一个临时解决方法,但由于问题的性质,该方法可能不适用于所有人。我们正在尽快修复此问题。

如果您在翻译 Yoast 内容时遇到问题,请尝试以下临时解决方法,并等待 Yoast Multilingual 的下一个版本,然后再提交支持请求。

临时解决方法

将此代码添加到您的主题 functions.php 中:

if ( ! is_admin() ) {
	add_filter( 'wpseo_title', 'wpml_wpseo_title', 10, 2 );
	add_filter( 'wpseo_opengraph_title', 'wpml_wpseo_title', 10, 2 );
}

function wpml_wpseo_title( $title, $presentation ) {
	switch ( $presentation->model->object_type ) {
		case 'post-type-archive':
			$key = 'title-ptarchive-' . $presentation->model->object_sub_type;
			break;
		case 'system-page':
			$key = 'title-' . $presentation->model->object_sub_type . '-wpseo';
			break;
		default:
			$key = false;
	}

	if ( $key ) {
		$option = get_option( 'wpseo_titles' );
		if ( isset( $option[ $key ] ) ) {
			$title = wpseo_replace_vars( $option[ $key ], $presentation );
		}
	}

	return $title;
}

if ( ! is_admin() ) {
	add_filter( 'wpseo_metadesc', 'wpml_wpseo_desc', 10, 2 );
	add_filter( 'wpseo_opengraph_desc', 'wpml_wpseo_desc', 10, 2 );
}

function wpml_wpseo_desc( $title, $presentation ) {
	switch ( $presentation->model->object_type ) {
		case 'post-type-archive':
			$key = 'metadesc-ptarchive-' . $presentation->model->object_sub_type;
			break;
		case 'system-page':
			$key = 'metadesc-' . $presentation->model->object_sub_type . '-wpseo';
			break;
		default:
			$key = false;
	}

	if ( $key ) {
		$option = get_option( 'wpseo_titles' );
		if ( isset( $option[ $key ] ) ) {
			$title = wpseo_replace_vars( $option[ $key ], $presentation );
		}
	}

	return $title;
}

add_filter( 'wpseo_save_indexable', function( $indexable ) {
		$args = [
			'element_id'   => $indexable->object_id,
			'element_type' => $indexable->object_type,
		];

		$language_code        = apply_filters( 'wpml_element_language_code', false, $args );
		$indexable->permalink = apply_filters( 'wpml_permalink', $indexable->permalink, $language_code, true );

		return $indexable;
} );

添加代码后,安装 Yoast Test Helper 插件。
前往 Tools > Yoast Test 并单击“Reset Indexables tables & migrations”。
最后,前往您的仪表板并单击提示您为站点建立索引的管理后台通知。

如果您有任何疑问或希望报告相关问题,请提及此已知问题,以便我们的团队为您提供高效的支持。

所有已知问题 →