使用Polylang来返回当前所选语言的术语。

问题描述 投票:0回答:1

我正在使用这个基本的循环来检索类别名称,我将在自定义的WooCommerce dashboard.php模板中返回这些结果,以便显示客户的订单历史。

$terms = get_the_terms( $product_id, 'product_cat' );
//var_dump ($terms);
if($items){
    foreach ( $terms as $term ) {
        //$cat_id = $term->id;
        $cat_name = $term->name; //returns category name, but only in original language booking was made in.
        }
    }
}

我在一个自定义的WooCommerce dashboard.php模板中返回这些结果,以便显示客户订单历史。代码工作,但它返回的类别名称($cat_name),但不是以当前选择的语言,只是以预订最初的语言。我尝试使用的函数是pll_get_term(),但我不确定实现它的最佳方式。

谢谢你的关注。

wordpress woocommerce polylang
1个回答
0
投票

在Polylang高级支持的帮助下,并纠正了我检索id的方式,我写错了,应该是这样。

$terms = get_the_terms( $product_id, 'product_cat' ); 


if($items){
    foreach ( $terms as $term ) {
    $cat_id = pll_get_term( $term->term_id, $current_lang );
    $cat_name = get_term( $cat_id )->name;
        }
    }
}

我在原帖中没有展示使用pll_get_term函数的尝试,但这个函数需要一个id作为参数,应该是term_id而不是id。

克里斯

© www.soinside.com 2019 - 2024. All rights reserved.