$translated_slug = apply_filters( 'wpml_get_translated_slug', string $slug, string $post_type, string $language_code, string $element_type );
- ประเภท
- filter
- เวอร์ชันของ WPML
- 3.2.3
คำอธิบาย
- ดึงข้อมูลคำแปล slug ของประเภทโพสต์หรืออนุกรมวิธานในภาษาที่ระบุ
- ค่าการแปลจะถูกบันทึกไว้ใน WPML → การตั้งค่า → การแปลประเภทโพสต์
อาร์กิวเมนต์
- $slug
- (string) (จำเป็น) ค่าที่ตั้งไว้ในอาร์กิวเมนต์ rewrite ของฟังก์ชัน register_post_type
- $post_type
- (string) (จำเป็น) ค่าพารามิเตอร์แรกของฟังก์ชัน register_post_type หรือ register_taxonomy
- $language_code
- (string) (ไม่บังคับ) ส่งคืนคำแปลในภาษานี้ ค่าเริ่มต้นคือ NULL ซึ่งจะส่งคืนภาษาปัจจุบัน
- $element_type
- (string) (ไม่บังคับ) ระบุว่า slug ที่แปลนั้นใช้กับประเภทโพสต์แบบกำหนดเองหรืออนุกรมวิธานที่กำหนดเอง ค่าที่ยอมรับคือ post หรือ taxonomy ค่าเริ่มต้นคือ post
ตัวอย่างการใช้งาน
ตัวอย่างประเภทโพสต์
ลงทะเบียนประเภทโพสต์ book ด้วย slug ที่กำหนดเอง my-book
ใน WPML → การตั้งค่า → การแปลประเภทโพสต์ ให้แปล slug ที่กำหนดเองเป็นภาษาสเปนด้วยค่า mi-libro
ใช้ฮุก wpml_get_translated_slug เพื่อรับคำแปลภาษาสเปนของ slug ที่กำหนดเองนี้ ซึ่งก็คือ mi-libro

// Register a "book" post type with the custom slug "my-book"
add_action( 'init', 'register_book_init' );
function register_book_init() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'rewrite' => array( 'slug' => 'my-book' ),
);
register_post_type( 'book', $args );
}
// Get the Spanish translation of the post type slug "my-book"
// This value is saved in WPML->Settings->Post Types Translation
$translated_slug = apply_filters( 'wpml_get_translated_slug', 'my-book', 'book' ,'es' , 'post');
// Return "mi-libro"
echo $translated_slug;
// Register a "genre" custom taxonomy to the "book" custom post type
add_action( 'init', 'register_genre_init', 0 );
function register_genre_init() {
register_taxonomy( 'genre', 'book', array(
'rewrite' => array( 'slug' => 'genre' )
) );
}
ตัวอย่างอนุกรมวิธาน
ลงทะเบียนประเภทอนุกรมวิธาน genre ด้วย slug ที่กำหนดเอง genre
ใน WPML → การแปลอนุกรมวิธาน ให้แปลเทอมอนุกรมวิธานเป็นภาษาสเปนด้วยค่า género
ใช้ฮุก wpml_get_translated_slug เพื่อรับคำแปลภาษาสเปนของ slug ที่กำหนดเองนี้ ซึ่งก็คือ género
// Get the Spanish translation of the custom taxonomy slug "genre" // This value is saved in WPML->Settings->Taxonomies Translation $translated_taxonomy_slug = apply_filters( 'wpml_get_translated_slug', 'genre', 'genre' ,'es', 'taxonomy' ); // Return "género" echo $translated_taxonomy_slug;
ฮุกที่เกี่ยวข้อง
wpml_media_url, wpml_media_id, force_suppress_filters, wpml_home_url, wpml_translate_single_string, wpml_translate_string, wpml_elements_without_translations, wpml_permalink, wpml_unfiltered_admin_string, wpml_element_link
ฮุก การดึงเนื้อหาที่ปรับให้เข้ากับท้องถิ่น ทั้งหมด 12 รายการ →