1. Một số hằng số thường dùng trong plugin WPML:
vd: Ngôn ngữ hiện tại là ngôn ngữ tiếng Việt thì khi bạn in các hằng số sau ra sẽ có được kết quả
ICL_LANGUAGE_CODE: => vi
ICL_LANGUAGE_NAME: => Việt Nam
Xem thêm các hằng số khác: https://wpml.org/documentation/support/wpml-coding-api/wpml-constants/
2. Chuyển ngôn ngữ trong function WordPress: ứng dụng khi sử dụng ajax ta cần truyền ngôn ngữ cần lấy khi thực hiện query
global $sitepress; $language_code = "vi"; $original_lang = ICL_LANGUAGE_CODE; $sitepress->switch_lang($language_code);
3. Cách lấy bản dịch của ngôn ngữ hiện tại trong WPML
- Lấy tất cả bản dịch
$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 );
}
Output sẽ trả về danh sách các bản dịch,
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"
}
Trong vd trên: thì website có 3 ngôn ngữ: Vi, En, Fr thì kết quả trả ra là 2 ngôn ngữ còn lại là En và Fr
- Lấy bản dịch theo ngôn ngữ chỉ định
// will return the post ID in the current language for post ID 27 echo apply_filters( 'wpml_object_id', 27, '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 English attachment ID for attachment ID 27. If the translation is missing it will return NULL echo apply_filters( 'wpml_object_id', 27, 'attachment', FALSE, 'en' );
4. Câu trúc bảng trong plugin WPML
Chi tiết: https://wpml.org/documentation/support/wpml-tables/