【发布时间】:2016-01-18 09:35:40
【问题描述】:
我正在使用 wordpress 注册菜单在我的产品存档页面上显示产品标签的无序列表。
如果可能,我想维护无序列表的层次结构,但是,我想将没有产品关联的列表项设为灰色,这样用户就不会被引导到没有产品的页面。
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.
}
}
}
}
【问题讨论】:
标签: php wordpress filter menu woocommerce