Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

php - How do you get wp_terms in current language only?

I am working on a piece of code used to create a woocommerce product in 2 languages and I have managed to do that with the product itself although I am having a problem with the categories for the 2 languages.

$all_cats  = get_terms(array(
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'name' => trim($category),
));

using

$sitepress->switch_lang($alt_lang, true);

to switch language and then doing the same query both return the same term_id.

Printing $sitepress->get_current_language() before each query shows the correct language being chosen.

enter image description here

I have also tried both of the first 2 options in case it defaults to one language, I have tried using apply_filters('wpml_object_id',...); and icl_object_id to no avail.

How would I force wordpress to provide me with the categories of the current language only and if there aren't return nothing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pass a language code to the wpml_object_id filter:

$current_language = apply_filters( 'wpml_current_language', NULL );

$translated_cat_ids = array();

foreach ( $all_cats as $term ) {
    $translated_cat_ids[] = apply_filters( 'wpml_object_id', $term->term_id, 'product_cat', false, $current_language );
}

Documentation: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605256


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...