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