Как отобразить только текущую категорию и подкатегории - PullRequest
1 голос
/ 15 апреля 2020

Как отобразить только текущую категорию продукта и подкатегории. Например, я нахожусь в категории A, и я хочу видеть только категорию и подкатегории для A. Где я нахожусь в некоторой подкатегории A, я хочу видеть также категорию 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>';
  }

1 Ответ

0 голосов
/ 16 апреля 2020
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 

Примечания

$ args

(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.
...