ナビゲーションをスキップする

You can get the current language by using two WPML hooks, wpml_current_language, and wpml_post_language_details.

The wpml_current_language is ideal for getting the site’s current language, while the wpml_post_language_details provides detailed information about a post’s language attributes.

How can I get the current language of my site?

The wpml_current_language hook is the recommended way to get the current language of your site. One way to use this hook is to create a shortcode that returns the current language as the 2-letter language code.

function get_language_shortcode() {
    return apply_filters( 'wpml_current_language', null );
}
add_shortcode( 'language', 'get_language_shortcode' );

With this code, you can use the ja shortcode anywhere on your site to display the code of the current language.

How can I get the current language of a post?

The wpml_post_language_details hook is used to get language details of a post using the post ID. This hook returns the language code, post locale, language text direction, language name and native name, and if the current language is different to the post’s default language. This hook can be useful if you want to create a custom language switcher.

For more information and an example, see the documentation page about the wpml_post_language_details hook.

How can I get other languages of a post?

Read more about how to get the other languages of the current post.