$translated_slug = apply_filters( 'wpml_get_translated_slug', string $slug, string $post_type, string $language_code, string $element_type );
- 类型
- filter
- 类别
- 获取本地化内容
- WPML 版本
- 3.2.3
说明
- 获取特定语言的文章类型或分类法别名翻译。
- 翻译值保存在WPML → 设置 → 文章类型翻译中。
参数
- $slug
- (string) (必填) 该值在 register_post_type 函数的 rewrite 参数中设置。
- $post_type
- (string) (必填) register_post_type 或 register_taxonomy 函数的第一个参数值。
- $language_code
- (string) (可选) 返回此语言的翻译。默认值为 NULL,即返回当前语言。
- $element_type
- (string) (可选) 指定翻译后的别名是应用于自定义文章类型还是自定义分类法。接受的值为 post 或 taxonomy。默认值为 post。
使用示例
文章类型示例
注册一个自定义别名为 my-book 的 book 文章类型。
在 WPML → 设置 → 文章类型翻译中,将该自定义别名翻译为西班牙语,值为 mi-libro。
使用 wpml_get_translated_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 的 genre 分类法类型。
在 WPML → 分类翻译中,将该分类法术语翻译为西班牙语,值为 género。
使用 wpml_get_translated_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