在父级的子类别中显示父级的其他子类别

时间:2018-11-21 12:37:40

标签: php wordpress woocommerce html-lists custom-taxonomy

我想要一个类别及其子类别的简单概述。单击父类别时,仅显示该类别的子类别。但是我也希望这些子类别在同一父级的不同子类别中显示。 我意识到queried_object_id();单击子类别时更改。但是如何让查询知道呢?

我的以下代码:

    <ul>
<?php
        $parentid = get_queried_object_id();
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;
                                //echo $category_id;
        echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' - '. $category_id .'</a></li>';
//echo $parentid;
if($parentid == $category_id) {
                                //$cat = get_queried_object();
        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );

        $sub_cats = get_categories( $args2 );

        if($sub_cats) {                     
                                    echo '<ul>';
            foreach($sub_cats as $sub_category) {
                                                    //echo $parentid;

               echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .' - '. $sub_category->term_id .'</a></li>';
                                                                //echo  $sub_category->name ;
            }
                                            echo '</ul>';
        }
}                               
    }       
}
?>
</ul>

3 个答案:

答案 0 :(得分:1)

为什么不简单地将woocommerce小部件与Wordpress the_widget()函数一起用于产品类别,因为您将通过这种方式获得所有信息:

MessageHandlingException

它将显示链接的产品类别的分层垂直列表。

  

借助the_widget()功能,您可以:
  -第二个参数参数在查询中进行更改
  -第3个参数参数会在html输出中进行更改

答案 1 :(得分:1)

我基于获取 $ parentid2 = get_queried_object();

来更改代码

注意:此代码位于/woocommerce/archive-product.php

并且我添加了带有不同参数的elseif语句。

也许有点太多,但是可以用

    <ul>
    <?php
      $parentid2 = get_queried_object(); 
      $parentid = get_queried_object_id();
      $taxonomy     = 'product_cat';
      $orderby      = 'name';  
      $show_count   = 0;      // 1 for yes, 0 for no
      $pad_counts   = 0;      // 1 for yes, 0 for no
      $hierarchical = 1;      // 1 for yes, 0 for no  
      $title        = '';  
      $empty        = 0;

      $args = array(
             'taxonomy'     => $taxonomy,
             'orderby'      => $orderby,
             'show_count'   => $show_count,
             'pad_counts'   => $pad_counts,
             'hierarchical' => $hierarchical,
             'title_li'     => $title,
             'hide_empty'   => $empty
      );
     $all_categories = get_categories( $args );
     foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

    if($parentid == $category_id) {
            $args2 = array(
                    'taxonomy'     => $taxonomy,
                    'child_of'     => 0,
                    'parent'       => $category_id,
                    'orderby'      => $orderby,
                    'show_count'   => $show_count,
                    'pad_counts'   => $pad_counts,
                    'hierarchical' => $hierarchical,
                    'title_li'     => $title,
                    'hide_empty'   => $empty
            );

            $sub_cats = get_categories( $args2 );

            if($sub_cats) {                     
                                        echo '<ul>';
                foreach($sub_cats as $sub_category) {
                                                        //echo $parentid;

                   echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
                                                                    //echo  $sub_category->name ;
                }
                                                echo '</ul>';
            }
    }

    elseif($parentid2->parent == $category_id) {
            $args2 = array(
                    'taxonomy'     => $taxonomy,
                    'child_of'     => 0,
                    'parent'       => $parentid2->parent,
                    'orderby'      => $orderby,
                    'show_count'   => $show_count,
                    'pad_counts'   => $pad_counts,
                    'hierarchical' => $hierarchical,
                    'title_li'     => $title,
                    'hide_empty'   => $empty
            );

            $sub_cats = get_categories( $args2 );

            if($sub_cats) {                     
                                        echo '<ul>';
                foreach($sub_cats as $sub_category) {
                                                        //echo $parentid;

                   echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
                                                                    //echo  $sub_category->name ;
                }
                                                echo '</ul>';
            }
    }



        }       
    }
    ?>
    </ul>

答案 2 :(得分:-1)

您可以通过

排除当前类别/术语
'exclude' => $catID

在参数中。