WPML
apply_filters( 'wpml_copy_post_to_language', int $post_id, string $target_language, bool $mark_as_duplicate )
ประเภท
filter
เวอร์ชันของ WPML
4.0.0

คำอธิบาย

ฟิลเตอร์ฮุกนี้ช่วยให้นักพัฒนาสามารถคัดลอกโพสต์ไปยังภาษาที่ระบุด้วย WPML โดยจะทำสำเนาโพสต์ไปยังภาษาปลายทาง และมีตัวเลือกให้ทำเครื่องหมายว่าเป็นสำเนาหรือเป็นการแปล

อาร์กิวเมนต์

$post_id
(int) (จำเป็น) ID ของโพสต์ที่จะคัดลอก
$target_language
(string) (จำเป็น) รหัสภาษาของภาษาปลายทางที่จะคัดลอกโพสต์ไป
$mark_as_duplicate
(bool) (จำเป็น) หากตั้งค่าเป็น true โพสต์ที่ทำสำเนาจะถูกทำเครื่องหมายว่าเป็นสำเนาของโพสต์ต้นฉบับ หากเป็น false จะถือว่าเป็นโพสต์ที่แปลแล้ว

ตัวอย่างการใช้งาน

ในตัวอย่างด้านล่าง เราใช้ฟิลเตอร์เพื่อคัดลอกโพสต์ที่มี 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

ฮุก การแทรกเนื้อหา ทั้งหมด 10 รายการ →