wp_list_categories()-在子类别页面中显示第一级类别,并且仅显示当前术语的子级 [英] wp_list_categories() - display first level categories and only current term's children in subcategory page

查看:67
本文介绍了wp_list_categories()-在子类别页面中显示第一级类别,并且仅显示当前术语的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WordPress.

I'm using WordPress.

具有多个类别及其子类别.在一般页面上,我将显示所有第一级类别.这是我的代码:

Have multiple categories with its subcategories. In general page, I'm displaying all first level categories. Here is my code:

$args = array(
   'type' => 'product-items',
   'child_of'  => 0,
   'parent'  => '',
   'order' => 'DESC',
   'hide_empty' => 0,
   'hierarchical' => 1,
   'exclude' => '',
   'include' => '',
   'number' => '',
   'taxonomy' => 'product-category',
   'pad_counts' => false,
   'depth' => 1,
   'title_li' => '' 
);
wp_list_categories($args);

单击并进入第一级类别,您只需要在该类别中查看其子类别.当我删除'depth'=>时1,选项,所有子项都出现在其父类别下,但对于页面速度/负载,在子页面中,我需要显示所有第一级类别,但仅显示当前类别的子项.

Once you click and go inside a first level category, you need to see only its subcategories there. When I'm removing 'depth' => 1, option, all children appear under their parent category but for page speed/load, in sub page I need to show all first-level categories, but only current category's children.

例如,我有以下3个类别:

For example, I have below 3 categories:

  • 类别1
  • 类别2
  • 类别3

想象一下,我单击类别1".现在是这样的:

Imagine I click on "Category 1". Now it is like this:

  • 类别1
    • 1的第一个子类别
    • 1的第二子类别
    • 第3个子类别,共1个
    • 第2个子类别
      • 第二个类别子项的第一个子项
      • 第二个类别子类别的第二个子类别
      • 第二个类别子类别的第三个子类别
      • 第3个子类别
      • 3的第二子类别
      • 3的第3子类别

      但是我需要它在子页面中像这样:

      But I need it to be like this in sub page:

      • 类别1
        • 1的第一个子类别
        • 1的第二子类别
        • 第3个子类别,共1个

        不确定如何使用 wp_list_categories()函数实现此目的.有什么想法吗?

        Not sure how to achieve this with wp_list_categories() function. Any ideas please?

        推荐答案

        如果使用2 get_terms()而不是wp_list_categories,那会更好.这将是更快和可定制的.一个用于父类别,另一个用于当前类别的子类别.这是工作示例:

        It would be better if you use 2 get_terms() instead of wp_list_categories. It would be faster and customizable. One for parent categories, another one for children of current category. Here is working example:

           function display_cats($cats,$current=0,$current_children=array()){
            $ret= '<ul>';
            foreach ($cats as $cs){
              $children=($current!=$cs->term_id)?'':display_cats($current_children);
              $ret.= '<li> <a href="'.get_term_link($cs->term_id).'"> '.$cs->name.'</a> '.$children.' </li>
              ';
            }
            $ret.= '</ul>';
            return $ret;
          }
        
        
          $current_cat=9;//for example
          $parents=get_terms('product_cat',array('taxonomy'=>'product_cat','echo'=>false,'depth'=>0));
          $current_children=get_terms('product_cat',array('taxonomy'=>'product_cat','child_of'=>  $current_cat ,'echo'=>false));
          echo display_cats($parents,$current_cat,$current_children);
        

        这篇关于wp_list_categories()-在子类别页面中显示第一级类别,并且仅显示当前术语的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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