- 状态
- 已解决
- 报告针对
- WPML SEO 2.1.0
- 解决于
- 2.2.0
- 相关插件/主题
- Rank Math SEO
- 主题标签
- Compatibility
问题概述
使用 Rank Math SEO 插件时,在默认语言中为产品或文章设置的主分类不会同步到其翻译中。即使您将 rank_math_primary_product_cat 自定义字段设置为复制,该问题依然存在。
临时解决方法
在继续操作之前,请确保对站点进行完整备份。
- 打开主题的 functions.php 文件。
- 添加以下代码:
// compsupp-6580: sync primary term and primary product terms for Rank Math add_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20, 4); function compsupp6580_filter_the_post_metadata($value, $postId, $key, $single) { $meta_keys_mapping = array( 'rank_math_primary_category' => 'category', 'rank_math_primary_product_cat' => 'product_cat', ); if (in_array($key, array_keys($meta_keys_mapping), true)) { remove_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20); $value = get_post_meta($postId, $key, true); add_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20, 4); $args = [ 'element_id' => $postId, 'element_type' => get_post_type($postId), ]; $language = apply_filters('wpml_element_language_code', false, $args); $value = apply_filters('wpml_object_id', $value, $meta_keys_mapping[$key], true, $language); if (!$single) { $value = [$value]; } } return $value; }