繁体   English   中英

您如何仅以当前语言获得 wp_terms?

[英]How do you get wp_terms in current language only?

我正在编写一段代码,用于以 2 种语言创建 woocommerce 产品,尽管我对 2 种语言的类别有疑问,但我已经设法用产品本身做到了这一点。

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

使用

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

切换语言然后执行相同的查询都返回相同的 term_id。

在每个查询之前打印$sitepress->get_current_language()会显示选择的正确语言。

在此处输入图像描述

我还尝试了前两个选项,以防它默认为一种语言,我尝试使用apply_filters('wpml_object_id',...); icl_object_id无济于事。

我将如何强制 wordpress 仅向我提供当前语言的类别,如果没有返回任何内容?

您可以将语言代码传递给wpml_object_id过滤器:

$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 );
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM