ข้ามการนำทาง

You can get other languages for a current post using several of WPML’s hooks. These hooks can help you get all other languages, a specific language, and current language of a post.

Getting other languages of a current post is a common way to build useful features for your site. For instance, you can build a custom language switcher with links to all the translations of the current post.

How can I get all other languages for a post?

You can get other languages of a post using three main hooks:

  • wpml_element_type, which allows you to get the element type for a post type or taxonomy as saved in the database
  • wpml_element_trid, which allows you to get the translation group ID (trid) of a translated element
  • wpml_get_element_translations, which allows you to get the translations of an element using the trid and the element type

In this example, we will get the other languages of a product, however this method works for any post type.

$post_id = 47;
 
$type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
$trid = apply_filters( 'wpml_element_trid', false, $post_id, $type );
 
$translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $type );
foreach ( $translations as $lang => $translation ) {
	var_dump( $translation );
}

This will output something like:

class stdClass#4886 (8) {
  public $translation_id => string(2) "60"
  public $language_code => string(2) "en"
  public $element_id => string(2) "47"
  public $source_language_code => NULL
  public $element_type => string(12) "post_product"
  public $original => string(1) "1"
  public $post_title => string(13) "Example  Product"
  public $post_status => string(7) "publish"
}
 
class stdClass#4877 (8) {
  public $translation_id => string(2) "61"
  public $language_code => string(2) "fr"
  public $element_id => string(2) "48"
  public $source_language_code => string(2) "en"
  public $element_type => string(12) "post_product"
  public $original => string(1) "0"
  public $post_title => string(15) "Exemple de produit"
  public $post_status => string(7) "publish"
}

You can see more information about the meaning of each field in our documentation about WPML’s tables.

How can I get a specific language for a post?

If you know the target language you want to get information for, you can convert the ID using the wpml_object_id hook. For example:

// 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 category 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' );

Once you have the post ID for the specific target language, you can use it to get any information about that post.

How can I get the current language of a post?

Read more about how to get the current language with WPML.