Showing other subcategories of parent when in subcategory of parent












1















I want a simple overview of categories and their subcategories. When clicking the parent category only subcategories of that category are shown. But I also want the subcategories to show when in a different subcategory of the same parent.
I realise the queried_object_id(); changes when clicking on the subcategory. But how do I let the query know?



My following code:



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









share|improve this question

























  • @LoicTheAztec you always have miracle solutions when it comes to this ;)

    – Solomax
    Nov 21 '18 at 12:38
















1















I want a simple overview of categories and their subcategories. When clicking the parent category only subcategories of that category are shown. But I also want the subcategories to show when in a different subcategory of the same parent.
I realise the queried_object_id(); changes when clicking on the subcategory. But how do I let the query know?



My following code:



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









share|improve this question

























  • @LoicTheAztec you always have miracle solutions when it comes to this ;)

    – Solomax
    Nov 21 '18 at 12:38














1












1








1








I want a simple overview of categories and their subcategories. When clicking the parent category only subcategories of that category are shown. But I also want the subcategories to show when in a different subcategory of the same parent.
I realise the queried_object_id(); changes when clicking on the subcategory. But how do I let the query know?



My following code:



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









share|improve this question
















I want a simple overview of categories and their subcategories. When clicking the parent category only subcategories of that category are shown. But I also want the subcategories to show when in a different subcategory of the same parent.
I realise the queried_object_id(); changes when clicking on the subcategory. But how do I let the query know?



My following code:



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






php wordpress woocommerce html-lists custom-taxonomy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:14









LoicTheAztec

88k1364101




88k1364101










asked Nov 21 '18 at 12:37









SolomaxSolomax

138111




138111













  • @LoicTheAztec you always have miracle solutions when it comes to this ;)

    – Solomax
    Nov 21 '18 at 12:38



















  • @LoicTheAztec you always have miracle solutions when it comes to this ;)

    – Solomax
    Nov 21 '18 at 12:38

















@LoicTheAztec you always have miracle solutions when it comes to this ;)

– Solomax
Nov 21 '18 at 12:38





@LoicTheAztec you always have miracle solutions when it comes to this ;)

– Solomax
Nov 21 '18 at 12:38












3 Answers
3






active

oldest

votes


















1














I changed the code based on getting $parentid2 = get_queried_object();



Note: this code is placed in /woocommerce/archive-product.php



and I added an elseif statement with different args.



Maybe a little bit too much but it works



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





share|improve this answer































    1














    Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget() function, as you will get everything this way:



     the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true )  );


    It will display a hierarchical vertical list of linked product categories.




    With the_widget() function you can with:

    - the 2nd parameter argument make changes in the Query

    - the 3rd parameter argument make changes in the html output







    share|improve this answer


























    • the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

      – Solomax
      Nov 21 '18 at 20:24













    • basically when in the subcategory get the queried_object and use it to compare

      – Solomax
      Nov 21 '18 at 20:26











    • @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

      – LoicTheAztec
      Nov 22 '18 at 0:13





















    -1














    you can exclude current category/term by



    'exclude' => $catID


    in the arguments.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412196%2fshowing-other-subcategories-of-parent-when-in-subcategory-of-parent%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I changed the code based on getting $parentid2 = get_queried_object();



      Note: this code is placed in /woocommerce/archive-product.php



      and I added an elseif statement with different args.



      Maybe a little bit too much but it works



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





      share|improve this answer




























        1














        I changed the code based on getting $parentid2 = get_queried_object();



        Note: this code is placed in /woocommerce/archive-product.php



        and I added an elseif statement with different args.



        Maybe a little bit too much but it works



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





        share|improve this answer


























          1












          1








          1







          I changed the code based on getting $parentid2 = get_queried_object();



          Note: this code is placed in /woocommerce/archive-product.php



          and I added an elseif statement with different args.



          Maybe a little bit too much but it works



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





          share|improve this answer













          I changed the code based on getting $parentid2 = get_queried_object();



          Note: this code is placed in /woocommerce/archive-product.php



          and I added an elseif statement with different args.



          Maybe a little bit too much but it works



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 20:30









          SolomaxSolomax

          138111




          138111

























              1














              Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget() function, as you will get everything this way:



               the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true )  );


              It will display a hierarchical vertical list of linked product categories.




              With the_widget() function you can with:

              - the 2nd parameter argument make changes in the Query

              - the 3rd parameter argument make changes in the html output







              share|improve this answer


























              • the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

                – Solomax
                Nov 21 '18 at 20:24













              • basically when in the subcategory get the queried_object and use it to compare

                – Solomax
                Nov 21 '18 at 20:26











              • @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

                – LoicTheAztec
                Nov 22 '18 at 0:13


















              1














              Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget() function, as you will get everything this way:



               the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true )  );


              It will display a hierarchical vertical list of linked product categories.




              With the_widget() function you can with:

              - the 2nd parameter argument make changes in the Query

              - the 3rd parameter argument make changes in the html output







              share|improve this answer


























              • the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

                – Solomax
                Nov 21 '18 at 20:24













              • basically when in the subcategory get the queried_object and use it to compare

                – Solomax
                Nov 21 '18 at 20:26











              • @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

                – LoicTheAztec
                Nov 22 '18 at 0:13
















              1












              1








              1







              Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget() function, as you will get everything this way:



               the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true )  );


              It will display a hierarchical vertical list of linked product categories.




              With the_widget() function you can with:

              - the 2nd parameter argument make changes in the Query

              - the 3rd parameter argument make changes in the html output







              share|improve this answer















              Why you dont simply use the woocommerce Widget for product categories with Wordpress the_widget() function, as you will get everything this way:



               the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true )  );


              It will display a hierarchical vertical list of linked product categories.




              With the_widget() function you can with:

              - the 2nd parameter argument make changes in the Query

              - the 3rd parameter argument make changes in the html output








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 0:12

























              answered Nov 21 '18 at 14:13









              LoicTheAztecLoicTheAztec

              88k1364101




              88k1364101













              • the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

                – Solomax
                Nov 21 '18 at 20:24













              • basically when in the subcategory get the queried_object and use it to compare

                – Solomax
                Nov 21 '18 at 20:26











              • @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

                – LoicTheAztec
                Nov 22 '18 at 0:13





















              • the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

                – Solomax
                Nov 21 '18 at 20:24













              • basically when in the subcategory get the queried_object and use it to compare

                – Solomax
                Nov 21 '18 at 20:26











              • @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

                – LoicTheAztec
                Nov 22 '18 at 0:13



















              the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

              – Solomax
              Nov 21 '18 at 20:24







              the widget works perfect but I want to show the subcategories of the parent category only. And when clicking on a subcategory the menu must stay in tact.

              – Solomax
              Nov 21 '18 at 20:24















              basically when in the subcategory get the queried_object and use it to compare

              – Solomax
              Nov 21 '18 at 20:26





              basically when in the subcategory get the queried_object and use it to compare

              – Solomax
              Nov 21 '18 at 20:26













              @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

              – LoicTheAztec
              Nov 22 '18 at 0:13







              @Solomax I have updated my answer adding , 'show_children_only' => true to the arguments array… may it's that what you want. Try it again please.

              – LoicTheAztec
              Nov 22 '18 at 0:13













              -1














              you can exclude current category/term by



              'exclude' => $catID


              in the arguments.






              share|improve this answer




























                -1














                you can exclude current category/term by



                'exclude' => $catID


                in the arguments.






                share|improve this answer


























                  -1












                  -1








                  -1







                  you can exclude current category/term by



                  'exclude' => $catID


                  in the arguments.






                  share|improve this answer













                  you can exclude current category/term by



                  'exclude' => $catID


                  in the arguments.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 14:21









                  janjuajanjua

                  295




                  295






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412196%2fshowing-other-subcategories-of-parent-when-in-subcategory-of-parent%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                      A Topological Invariant for $pi_3(U(n))$