apply_filters( 'wpml_copy_post_to_language', int $post_id, string $target_language, bool $mark_as_duplicate )
- Loại
- filter
- Danh mục
- Chèn nội dung
- Phiên bản WPML
- 4.0.0
Miêu tả
Hook bộ lọc này cho phép các nhà phát triển sao chép một bài viết sang một ngôn ngữ được chỉ định bằng WPML. Nó nhân bản bài viết sang ngôn ngữ đích và cung cấp tùy chọn để đánh dấu nó là bản nhân bản hoặc bản dịch.
Đối số
Ví dụ sử dụng
Trong ví dụ dưới đây, chúng tôi sử dụng bộ lọc để sao chép một bài viết có ID là 1 sang tiếng Pháp và đánh dấu nó là bản nhân bản của bài viết gốc.
Ví dụ
$duplicate_post_id = apply_filters( 'wpml_copy_post_to_language', 1, 'fr', true );
Trong ví dụ nâng cao mà bạn thấy dưới đây, chúng tôi trình bày cách tự động nhân bản một bài viết nhất định sang tất cả các ngôn ngữ đang hoạt động khác trên trang web WPML của bạn, ngoại trừ ngôn ngữ gốc của bài viết.
Ví dụ nâng cao
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
);
}
}
}
Các hook liên quan
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