1
我正试图在Wordpress中为特定的用户角色显示一个类别。 我发现这一点的代码是可行的,因为它没有显示类别产品时没有登录或当我有不同的用户角色。有条件地隐藏一些Woocommerce产品类别和wpml
但我遇到的问题如下: 该网站正在使用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');
所以英语类别编号为127和荷兰语它的128 有人可以帮助我得到这个工作了?
希望任何人都可以帮助我吗?
更新
的英国和荷兰的语言,现在只显示该类别时,用户角色是Wholeseller。但我的网站上有更多的语言。
下面是完整的列表与相应的类别ID:
English (en) => 117
Dutch (nl) ===> 118
French (fr) ==> 131
Italian (it) => 134
Spanish (es) => 137
German (de) ==> 442
我怎样才能使它超过2种语言的工作?
谢谢你帮我看看这个!我已将代码添加到我的functions.php中,但它不起作用。我一直使用荷兰语在商店页面上看到隐藏类别的产品。我尝试添加一些其他类别的ID,但它仍然不起作用。有任何想法吗? @LoicTheAztec – 123MijnWebsite
非常感谢你! – 123MijnWebsite
嗨@LoiTheAztec非常感谢你!现在唯一的一点是,当用户角色不是畅销商时,它将只显示ID为127的类别,但它需要反过来呢?所以当用户角色是畅销商时,它只需显示类别127,128。 – 123MijnWebsite