0
我使用wordpress註冊菜單在我的產品檔案頁面上顯示產品標籤的無序列表。Woocommerce使用產品標籤使用wp_nav_menu_objects過濾器隱藏菜單項
如果可能,我想維護無序列表的層次結構,但是,我想將沒有與它們關聯的產品的列表項灰化,以便用戶不會導致沒有產品的頁面。
無序列表看起來像這樣的wordpress後做它的東西,所以每個錨有一個標題,等於標籤名稱:
<ul id="menu-themes" class="menu">
<li>
<a href='#' title='fantasy'>Fantasy</a>
</li>
<li>
<a href='#' title='science'>Science</a>
<ul class='sub-menu'>
<li>
<a href='#' title='space'>Space</a>
</li>
</ul>
</li>
</ul>
我使用的過濾器來改變每個錨的href是適當。這是我爲了改變這種特定菜單的錨鏈接使用的過濾器:
function change_menu($items, $args){
if($args->theme_location == "themes"){
foreach($items as $item){
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
$item->url = $current_url . '/?product_tag=' . $item->title;
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'change_menu', 10, 2);
我已經得到了所有相關的變量列表使用打印出:
function woocommerce_product_loop_tags() {
global $post, $product;
echo $product->get_tags();
}
現在讓我們來說說例如,這個函數只能回顯空間。有沒有一種方法可以進一步過濾菜單以隱藏所有不等於空間的菜單項?
我想這將是這樣的:
function filter_menu_by_tags($items, $args){
//set scope of $product variable
global $product;
//this if statement makes sure only the themes menu is affected.
if($args->theme_location == "themes"){
//loop through each menu item
foreach($items as $item){
if($item->title does not match any of the tags in $product->get_tags()){
//then add a special class to the list item or anchor tag
}
else{
//do nothing and let it print out normally.
}
}
}
}
這絕對是正確的方向邁出的一步:d謝謝。 '$ product-> get_tags()'不會返回一個數組,但是'!in_array()'會拋出一個錯誤,因爲它返回一個字符串。你知道如何使它成爲一個數組嗎?我相信'!in_array()'應該是正確的答案:) –
我在看woocommerce的參考(不能說什麼版本),它說的用法是:'$ array = WC_Product :: get_tags($ sep,$之前,$後);'它應該返回一個數組 –
是的,我也看到,但是,我得到這個錯誤:警告:in_array()期望參數2是數組,字符串中給出..... (目錄位置) –