apply_filters( 'wpml_object_id', int $element_id, string $element_type, bool $return_original_if_missing, mixed $ulanguage_code )
- ประเภท
- filter
- เวอร์ชันของ WPML
- 3.2
คำอธิบาย
ส่งคืนค่า ID ขององค์ประกอบในภาษาปัจจุบันหรือในภาษาอื่นที่ระบุ
ตามค่าเริ่มต้น WPML จะปรับ ID โดยอัตโนมัติเพื่อแสดงองค์ประกอบของธีมทั้งหมดในภาษาปัจจุบัน การตั้งค่านี้จะถูก “เปิด” ตามค่าเริ่มต้นในหน้า WPML → ภาษา แต่ถ้าคุณกำลังสร้างธีมของคุณเองตั้งแต่เริ่มต้น ให้ใช้ฟังก์ชัน wpml_object_id เพื่อปรับ ID ด้วยตนเอง วิธีการนี้จะมีประสิทธิภาพมากกว่าในแง่ของการประมวลผล
อาร์กิวเมนต์
- $element_id
- (int) (จำเป็น) ID ของประเภทโพสต์ (โพสต์, หน้า, ไฟล์แนบ, โพสต์แบบกำหนดเอง) หรือเทอมของอนุกรมวิธาน (แท็ก, หมวดหมู่, อนุกรมวิธานที่กำหนดเอง) ที่จะกรอง
- $element_type
- (string) (จำเป็น) ประเภทขององค์ประกอบที่ ID นี้สังกัดอยู่ สามารถเป็น post, page, {custom post type key}, nav_menu, nav_menu_item, category, post_tag, {custom taxonomy key}
- $return_original_if_missing
- (bool) (ไม่บังคับ) หากตั้งค่าเป็น true จะส่งคืนค่าเสมอ (ค่าต้นฉบับ หากไม่มีการแปล) ค่าเริ่มต้นคือ FALSE
- $ulanguage_code
- (mixed) (ไม่บังคับ) หากไม่มีหรือเป็น NULL จะใช้ภาษาปัจจุบัน หากตั้งค่าเป็นรหัสภาษา จะส่งคืนคำแปลสำหรับรหัสภาษานั้น หรือส่งคืนค่าต้นฉบับหากไม่มีการแปลและ $return_original_if_missing ถูกตั้งค่าเป็น TRUE ค่าเริ่มต้นคือ NULL
ตัวอย่างการใช้งาน
ตัวอย่างพื้นฐาน
// will return the post ID in the current language for post ID 1 echo apply_filters( 'wpml_object_id', 1, 'post' ); // will return the category ID in the current language for categoy ID 4. If the translation is missing it will return the original (here: category ID 4) echo apply_filters( 'wpml_object_id', 4, 'category', TRUE ); // will return the German attachment ID for attachment ID 25. If the translation is missing it will return NULL echo apply_filters( 'wpml_object_id', 25, 'attachment', FALSE, 'de' );
ตัวอย่างที่ขั้นสูงขึ้น
/**
* Returns the translated object ID(post_type or term) or original if missing
*
* @param $object_id integer|string|array The ID/s of the objects to check and return
* @param $type the object type: post, page, {custom post type name}, nav_menu, nav_menu_item, category, tag etc.
* @return string or array of object ids
*/
function my_translate_object_id( $object_id, $type ) {
$current_language= apply_filters( 'wpml_current_language', NULL );
// if array
if( is_array( $object_id ) ){
$translated_object_ids = array();
foreach ( $object_id as $id ) {
$translated_object_ids[] = apply_filters( 'wpml_object_id', $id, $type, true, $current_language );
}
return $translated_object_ids;
}
// if string
elseif( is_string( $object_id ) ) {
// check if we have a comma separated ID string
$is_comma_separated = strpos( $object_id,"," );
if( $is_comma_separated !== FALSE ) {
// explode the comma to create an array of IDs
$object_id = explode( ',', $object_id );
$translated_object_ids = array();
foreach ( $object_id as $id ) {
$translated_object_ids[] = apply_filters ( 'wpml_object_id', $id, $type, true, $current_language );
}
// make sure the output is a comma separated string (the same way it came in!)
return implode ( ',', $translated_object_ids );
}
// if we don't find a comma in the string then this is a single ID
else {
return apply_filters( 'wpml_object_id', intval( $object_id ), $type, true, $current_language );
}
}
// if int
else {
return apply_filters( 'wpml_object_id', $object_id, $type, true, $current_language );
}
}
ฮุกที่เกี่ยวข้อง
wpml_media_url, wpml_media_id, force_suppress_filters, wpml_get_translated_slug, wpml_home_url, wpml_translate_single_string, wpml_translate_string, wpml_elements_without_translations, wpml_permalink, wpml_unfiltered_admin_string
ฮุก การดึงเนื้อหาที่ปรับให้เข้ากับท้องถิ่น ทั้งหมด 12 รายการ →