Php 如何仅显示当前类别和子类别

Php 如何仅显示当前类别和子类别,php,wordpress,woocommerce,categories,product,Php,Wordpress,Woocommerce,Categories,Product,如何仅显示当前产品类别和子类别。 例如,我在一个类别中,我只想看到一个类别和一个类别的子类别。 当我在某个子类别A中时,我想看到A的一个类别和所有子类别 $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 0 ); $product_cat = get_terms( $args );

如何仅显示当前产品类别和子类别。 例如,我在一个类别中,我只想看到一个类别和一个类别的子类别。 当我在某个子类别A中时,我想看到A的一个类别和所有子类别

  $args = array(
          'taxonomy' => 'product_cat',
          'hide_empty' => false,
          'parent'   => 0
      );
  $product_cat = get_terms( $args );

  foreach ($product_cat as $parent_product_cat)
  {

  echo '
      <ul>
        <li><a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
        <ul>
          ';
  $child_args = array(
              'taxonomy' => 'product_cat',
              'hide_empty' => false,
              'parent'   => $parent_product_cat->term_id
          );
  $child_product_cats = get_terms( $child_args );
  foreach ($child_product_cats as $child_product_cat)
  {
    echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
  }

  echo '</ul>
      </li>
    </ul>';
  }
$args=array(
“分类法”=>“产品分类”,
“hide_empty”=>false,
“父项”=>0
);
$product\U cat=获取条款($args);
foreach($product\U cat作为$parent\U product\U cat)
{
回声'
    • '; $child_args=数组( “分类法”=>“产品分类”, “hide_empty”=>false, “父项”=>$parent\u product\u cat->term\u id ); $child\u product\u cats=获取条款($child\u args); foreach($child\u product\u cat作为$child\u product\u cat) { 回音“
    • ”; } 回声'
'; }
如果(是_category()){
$cat=get_query_var('cat');
$this\u category=get\u category($cat);
$this_category=wp_list_categories($hide_empty=0&hierarchy=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of=’。$this_categority->cat_id.&echo=0)//根据需要更改参数
如果($this_category!='
  • 无类别。
  • ')) { 回声“产品”; 回显“
      ”.$此类别。“
    ”; } }//如果结束
    注释

    $args

    (数组|字符串)(可选)可选参数数组。有关其他接受参数的信息,请参见get_categories()、get_terms()和WP_Term_Query::_construct()。
    “当前类别”
    (int | array)类别ID或类别ID数组,应获取“当前cat”类。默认值为0。
    “深度”
    (int)类别深度。用于制表符缩进。默认值为0。
    “回声”
    (bool | int)是否回显或返回生成的标记。接受0、1或它们的布尔等价项。默认值1。
    “排除”
    (数组|字符串)要排除的术语ID的数组或逗号/空格分隔字符串。如果$hierarchy为true,则$exclude terms的后代也将被排除;请参见$exclude_树。请参阅获取术语()。
    “排除树”
    (数组|字符串)要排除的术语ID的数组或逗号/空格分隔字符串,以及它们的后代。请参阅获取术语()。
    “饲料”
    (字符串)用于提要链接的文本。默认“所有在[cat name]下归档的帖子的提要”。
    “feed_图像”
    (字符串)用于提要链接的图像的URL。
    “饲料类型”
    (字符串)馈送类型。用于构建提要链接。请参阅get_term_feed_link()。默认空字符串(默认提要)。
    '如果为空,则隐藏标题'
    (bool)如果列表中没有术语,是否隐藏$title_li元素。默认为false(标题将始终显示)。
    “分离器”
    (字符串)链接之间的分隔符。默认值
    。 “显示计数” (bool | int)是否包括职位计数。接受0、1或它们的布尔等价项。默认值为0。 “显示所有选项” (字符串)显示所有类别的文本。 '显示选项\u无' (字符串)要为“无类别”选项显示的文本。默认为“无类别”。 “风格” (字符串)用于显示类别列表的样式。如果是“列表”,则类别将作为无序列表输出。如果保留为空或其他值,则类别将由
    标记分隔。默认的“列表”。 “标题(u li)” (字符串)用于列表标题
  • 元素的文本。传递一个空字符串以禁用。默认的“类别”。 “使用描述作为标题” (bool | int)是否将类别描述用作标题属性。接受0、1或它们的布尔等价项。默认值1。
  • 如果(是_category()){
    $cat=get_query_var('cat');
    $this\u category=get\u category($cat);
    $this_category=wp_list_categories($hide_empty=0&hierarchy=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of=’。$this_categority->cat_id.&echo=0)//根据需要更改参数
    如果($this_category!='
  • 无类别。
  • ')) { 回声“产品”; 回显“
      ”.$此类别。“
    ”; } }//如果结束
    注释

    $args

    (数组|字符串)(可选)可选参数数组。有关其他接受参数的信息,请参见get_categories()、get_terms()和WP_Term_Query::_construct()。
    “当前类别”
    (int | array)类别ID或类别ID数组,应获取“当前cat”类。默认值为0。
    “深度”
    (int)类别深度。用于制表符缩进。默认值为0。
    “回声”
    (bool | int)是否回显或返回生成的标记。接受0、1或它们的布尔等价项。默认值1。
    “排除”
    (数组|字符串)要排除的术语ID的数组或逗号/空格分隔字符串。如果$hierarchy为true,则$exclude terms的后代也将被排除;请参见$exclude_树。请参阅获取术语()。
    “排除树”
    (数组|字符串)要排除的术语ID的数组或逗号/空格分隔字符串,以及它们的后代。请参阅获取术语()。
    “饲料”
    (字符串)用于提要链接的文本。默认“所有在[cat name]下归档的帖子的提要”。
    “feed_图像”
    (字符串)用于提要链接的图像的URL。
    “饲料类型”
    (字符串)馈送类型。用于构建提要链接。请参阅get_term_feed_link()。默认空字符串(默认提要)。
    '如果为空,则隐藏标题'
    (bool)如果列表中没有术语,是否隐藏$title_li元素。默认为false(标题将始终显示)。
    “分离器”
    (字符串)链接之间的分隔符。默认值
    。 “显示计数” (bool | int)是否包括职位计数。接受0、1或它们的布尔等价项。默认值为0。 “显示所有选项” (字符串)显示所有类别的文本。 '显示选项\u无' (字符串)要为“无类别”选项显示的文本。默认为“无类别”。 “风格” (字符串)用于显示类别列表的样式。如果是“列表”,则类别将作为uno输出
    if (is_category()) {
        $cat = get_query_var('cat');
        $this_category = get_category($cat);
        $this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0"); // Change your parameters as your requirement
    
        if($this_category !='<li>No categories.</li>')
        {
         echo '<h3>Products</h3>'; 
         echo '<ul>'.$this_category.'</ul>'; 
        }
    }  // if end 
    
    
    (array|string) (Optional) Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() for information on additional accepted arguments.
    
        'current_category'
        (int|array) ID of category, or array of IDs of categories, that should get the 'current-cat' class. Default 0.
        'depth'
        (int) Category depth. Used for tab indentation. Default 0.
        'echo'
        (bool|int) Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents. Default 1.
        'exclude'
        (array|string) Array or comma/space-separated string of term IDs to exclude. If $hierarchical is true, descendants of $exclude terms will also be excluded; see $exclude_tree. See get_terms().
        'exclude_tree'
        (array|string) Array or comma/space-separated string of term IDs to exclude, along with their descendants. See get_terms().
        'feed'
        (string) Text to use for the feed link. Default 'Feed for all posts filed under [cat name]'.
        'feed_image'
        (string) URL of an image to use for the feed link.
        'feed_type'
        (string) Feed type. Used to build feed link. See get_term_feed_link(). Default empty string (default feed).
        'hide_title_if_empty'
        (bool) Whether to hide the $title_li element if there are no terms in the list. Default false (title will always be shown).
        'separator'
        (string) Separator between links. Default <br />.
        'show_count'
        (bool|int) Whether to include post counts. Accepts 0, 1, or their bool equivalents. Default 0.
        'show_option_all'
        (string) Text to display for showing all categories.
        'show_option_none'
        (string) Text to display for the 'no categories' option. Default 'No categories'.
        'style'
        (string) The style used to display the categories list. If 'list', categories will be output as an unordered list. If left empty or another value, categories will be output separated by <br> tags. Default 'list'.
        'title_li'
        (string) Text to use for the list title <li> element. Pass an empty string to disable. Default 'Categories'.
        'use_desc_for_title'
        (bool|int) Whether to use the category description as the title attribute. Accepts 0, 1, or their bool equivalents. Default 1.