WPML
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

모든 현지화된 콘텐츠 가져오기 훅 12개 →