- 状态
- 已解决
问题概述
对于某些 ACF 字段类型,无法正确复制内容。
临时解决方法
如果您正在使用本文“完整说明”部分中列出的任何字段,您应该将此代码片段添加到主题的functions.php文件中,以确保复制功能正常工作。
function wpml_fix_acf_duplication( $value_to_filter, $target_language, $meta_data ) {
// Field names should be placed into following array.
$fields = array(
'test_taxonomy' => 'some_taxonomy_name',
'test_image' => 'attachment',
'test_file' => 'attachment',
'test_gallery' => 'attachment',
'test_post_object' => 'post',
'test_page_link' => 'page'
);
if ( array_key_exists( $meta_data['key'], $fields ) ) {
$value = maybe_unserialize( $value_to_filter );
if ( is_array( $value ) ) {
// If custom field value contain array of IDs to translate, usually used for gallery and taxonomy field types.
$translated_ids = array();
foreach ( $value as $v ) {
$translated_ids[] = apply_filters( 'wpml_object_id', $v, $fields[ $meta_data['key'] ], true, $target_language );
}
return maybe_serialize( $translated_ids );
} else {
// If custom field value contain single ID to translate, usually used for image and file field types.
$translated_id = apply_filters( 'wpml_object_id', $value, $fields[ $meta_data['key'] ], true, $target_language );
return $translated_id;
}
}
return $value_to_filter;
}
add_filter( 'wpml_duplicate_generic_string', 'wpml_fix_acf_duplication', 999, 3 );
注意:您应该将数组的key => value对替换为正确的值。
将“test_taxonomy”、“test_image”、“test_file”等键替换为您站点的字段名称,如下面的图像示例所示。

相应地替换“some_taxonomy_name”、“attachment”、post、page 等值。
这是 ID 所属的元素类型。可以是post、page、attachment、{custom post type key}、nav_menu、nav_menu_item、category、post_tag、{custom taxonomy key}
有关更多信息,请在此处查看我们的钩子文档。