apply_filters( 'wpml_copy_post_to_language', int $post_id, string $target_language, bool $mark_as_duplicate )
- 类型
- filter
- 类别
- 插入内容
- WPML 版本
- 4.0.0
说明
此过滤器钩子允许开发者使用 WPML 将文章复制到指定语言。它将文章复制到目标语言,并提供将其标记为复制或翻译的选项。
参数
使用示例
在下面的示例中,我们使用过滤器将 ID 为 1 的文章复制为法语,并将其标记为原始文章的副本。
示例
$duplicate_post_id = apply_filters( 'wpml_copy_post_to_language', 1, 'fr', true );
在下面的高级示例中,我们演示了如何将特定文章自动复制到您的 WPML 网站上的所有其他活动语言,不包括该文章的原始语言。
高级示例
function myplugin_duplicate_copy_post_to_secondary_languages( $post_id, $mark_as_duplicate = false ) {
// Retrieve the post's element type, trid, and existing translations
$element_type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
$trid = apply_filters( 'wpml_element_trid', null, $post_id, $element_type );
$translations = (array) apply_filters( 'wpml_get_element_translations', [], $trid, $element_type );
if ( ! $translations ) {
// Handle error or invalid values accordingly
return;
}
// Retrieve the active languages
$wpml_languages = (array) apply_filters('wpml_active_languages', [], 'orderby=id&order=desc');
foreach ( $wpml_languages as $language ) {
// Duplicate the post if no translation exists for the given language
if ( ! isset( $translations[ $language['language_code'] ] ) ) {
apply_filters(
'wpml_copy_post_to_language',
$post_id,
$language['language_code'],
$mark_as_duplicate
);
}
}
}
相关钩子
wpml_set_element_language_details, wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_delete_package, wpml_import_get_trid_from_imported_translations, wpml_register_string, wpml_register_single_string, wpml_show_package_language_ui, wpml_delete_package_action