3

I'm not sure how i do this, i have set it up correctly in the back end so for example i have a category 'bars and pubs' and i have 3 other categories 'x' 'y' 'z' and all there parents are bars and pubs. I want to achieve something similar when i click on the parent category it will then show the child categories.

 <div class=" cus-fw-tabs">
      <ul>
      <li><a href="#tabs-1">All</a></li>
      <?php

      $args = array(
            'orderby'           => 'ASC',
            'order'             => 'ASC',
            'hide_empty'        => false,
            'exclude'           => array(),
            'exclude_tree'      => array(),
            'include'           => $term_arr,
            'number'            => '',
            'fields'            => 'all',
            'slug'              => '',
            'parent'            => '',
            'hierarchical'      => true,
            'child_of'          => 0,
            'childless'         => false,
            'get'               => '',
            'name__like'        => '',
            'description__like' => '',
            'pad_counts'        => false,
            'offset'            => '',
            'search'            => '',
            'cache_domain'      => 'core'
      );


      $terms = get_terms('category',$args);
    if(!empty($terms)) {
        $i=2;
        foreach($terms as $term){


        ?>
            <li class="tabs-<?php echo $term->term_id; ?>">
                          <a class="term-heading" href="#tabs-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></a>
                        </li>
            <?php

            $i++;



        }

    }
            ?>


      </ul>
      </div>

1 Answer 1

1

Grab the parent category $category = get_category_by_slug( 'category-name' );

Set whatever args you need for your post query, but make sure to include the child_of arg

$args = array(
    'type'                     => 'post',
    'child_of'                 => $category->term_id,
    'hierarchical'             => 1,
    'taxonomy'                 => 'category',
);

Get your subcategories based on these args $child_categories = get_categories($args);

You'll get an associative array back, which you can loop over using foreach or whatever iterative function you want. If you want a more detailed solution to a specific problem in terms of a front-end application of this, feel free to update your question or post a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.