【发布时间】:2017-08-16 13:03:05
【问题描述】:
我正在尝试仅针对 Wordpress 中的特定用户角色显示类别。 我发现这段代码有效,因为它在未登录或我具有不同用户角色时不显示类别产品。
但我遇到的问题如下: 该网站使用 WPML,而我的代码仅适用于英语。但不适用于其他语言。所以我添加了测试另一个类别 ID,它是相同的类别,但只有这个是荷兰语,所以我期待它适用于英语和荷兰语,但它不会适用于英语。
我现在使用的代码是:
function wholeseller_role_cat( $q ) {
// Get the current user
$current_user = wp_get_current_user();
// Displaying only "Wholesale" category products to "whole seller" user role
if ( in_array( 'wholeseller', $current_user->roles ) ) {
// Set here the ID for Wholesale category
$q->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => '127,128', // your category ID
)
) );
// Displaying All products (except "Wholesale" category products)
// to all other users roles (except "wholeseller" user role)
// and to non logged user.
} else {
// Set here the ID for Wholesale category
$q->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => '127,128', // your category ID
'operator' => 'NOT IN'
)
) );
}
}
add_action( 'woocommerce_product_query', 'wholeseller_role_cat' );
所以英语类别 id 是 127,荷兰语是 128。 有人可以帮我恢复这个工作吗?
希望有人能帮我解决这个问题吗?
更新
英语和荷兰语现在仅在用户角色为批发商时显示类别。但是我的网站上有更多的语言。
这里是完整的列表以及相应的类别 ID:
English (en) => 117
Dutch (nl) ===> 118
French (fr) ==> 131
Italian (it) => 134
Spanish (es) => 137
German (de) ==> 442
如何使它适用于 2 种以上的语言?
【问题讨论】:
标签: php wordpress woocommerce categories wpml