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) (必填) 要筛选的文章类型(post、page、attachment、custom post)或分类法术语(tag、category、custom taxonomy)的 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