从 WordPress 菜单隐藏 WooCommerce 空产品类别 [英] Hide WooCommerce empty product categories from WordPress menu

查看:58
本文介绍了从 WordPress 菜单隐藏 WooCommerce 空产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于 在 Wordpress 菜单中按名称 ASC 对 WooCommerce 产品类别子菜单项进行排序 回答我上一个问题的代码,我能够自动将子子类别显示为 WordPress 导航菜单中某些产品类别的菜单子项.

Based on Sort WooCommerce product category sub menu items by name ASC in Wordpress menu answer code to my previous question, I am able to automatically display child subcategories as menu sub items for some product categories in my WordPress navigation menu.

我的问题是:是否可以隐藏空子类别(子菜单项)?

My question is: Is it possible to hide empty subcategories (sub menu items)?

如果可能的话,可能隐藏只包含一种售罄产品的类别.

推荐答案

您可以另外使用 get_terms() 带有以下参数:

You can use additionally get_terms() with the following parameters:

  • 'include' 包含来自 get_term_children() 函数的所有子术语,作为术语 ID 的逗号分隔字符串.
  • 'orderby' 带有 'name' 属性以按名称对术语进行排序,
  • 'order' 带有 'ASC' 属性以对术语进行升序排序,
  • 'hide_empty'带有 true 属性以不包含空术语.
  • 'include' to include all child terms from get_term_children() function as a coma separated string of term ids.
  • 'orderby' with 'name' attribute to sort terms by name,
  • 'order' with 'ASC' attribute to sort terms ascending,
  • 'hide_empty'with true attribute to not include empty terms.

因此排序将直接进行,无需 foreach 循环,您还将直接获得 WP_Term 对象数组.

So sorting will be made directly without requiring a foreach loop and you will get also directly an array of WP_Term objects.

重新访问的代码:

add_filter( 'wp_get_nav_menu_items', 'custom_submenu_product_categories', 10, 3 );
function custom_submenu_product_categories( $items, $menu, $args ) {
    // don't add child categories in administration of menus
    if (is_admin()) {
        return $items;
    }

    $taxonomy = 'product_cat';

    foreach ($items as $index => $post) {

        if ( $taxonomy !== $post->object ) {
            continue;
        }

        $children_terms_ids = get_term_children( $post->object_id, $taxonomy );

        if( ! empty($children_terms_ids) ) {
            $sorted_terms       = get_terms(array(
                'taxonomy'   => $taxonomy,
                'include'    => implode(',', $children_terms_ids),
                'orderby'    => 'name',
                'order'      => 'ASC',
                'hide_empty' => true,
            ));

            // Loop through sorted child terms to set them as sorted sub menu items
            foreach ( $sorted_terms as $index2 => $child_term ) {
                $item = new \stdClass();

                $item->title            = $child_term->name;
                $item->url              = get_term_link( $child_term, $taxonomy );
                $item->menu_order       = 500 * ($index + 1) + $index2;
                $item->post_type        = 'nav_menu_item';
                $item->post_status      = 'published';
                $item->post_parent      = $post->ID;
                $item->menu_item_parent = $post->ID;
                $item->type             = 'custom';
                $item->object           = 'custom';
                $item->description      = '';
                $item->object_id        = 0;
                $item->db_id            = 0;
                $item->ID               = 0;
                $item->position         = 0;
                $item->classes          = array();
                $item->target           = ''; // <= Missing - Mandatory to avoid an error
                $item->xfn              = ''; // <= Missing - Mandatory to avoid an error

                $items[] = $item;
            }
        }
    }
    return $items;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

抱歉,现在无法隐藏仅包含一种已售罄产品的子类别.

Now is not really possible to hide subcategories that contains only one product that are Sold Out, sorry.

这篇关于从 WordPress 菜单隐藏 WooCommerce 空产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆