【问题标题】:Woocommerce use product tags to hide menu items using wp_nav_menu_objects filterWoocommerce 使用产品标签使用 wp_nav_menu_objects 过滤器隐藏菜单项
【发布时间】: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


    【解决方案1】:

    $product-&gt;get_tags() 返回一个标签数组。您可以使用 PHP in_array() 函数来检查您的标题是否在列表中:

    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( !in_array($item->title, $product->get_tags()) ){
                    // Title is not in_array Tags
                    //then add a special class to the list item or anchor tag
                }
                else{
                     // Title is in_array Tags
                     //do nothing and let it print out normally.
                }
            }
        }
    }
    

    【讨论】:

    • 这绝对是朝着正确方向迈出的一步 :D 谢谢。 $product-&gt;get_tags() 不返回数组,但!in_array() 会抛出错误,因为它返回一个字符串。你知道如何把它变成一个数组吗?我相信!in_array() 应该是正确的答案:)
    • 我正在查看 woocommerce 的参考资料(不能说是什么版本),它说用法是:$array = WC_Product::get_tags( $sep, $before, $after ); 它应该返回一个数组
    • 是的,我也看到了,但是,我收到了这个错误:警告:in_array() 期望参数 2 是数组,字符串在.....(目录位置)
    • 如果你print_r( $product-&gt;get_tags() ); 输出是什么?
    • 这很残酷:您可以在 foreach $arr_tags = explode( ", ", $product-&gt;getTags() ); 之前将字符串分解为一个数组,然后检查它是否在数组 if( !in_array($item-&gt;title, $arr_tags) ){ ...... }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2021-01-16
    • 2018-04-23
    • 2013-12-05
    • 1970-01-01
    • 2022-01-12
    • 2021-06-13
    相关资源
    最近更新 更多