Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Luke Lee
Luke Lee
7,577 Points

Show subcategories only for selected category

I understand how work.php and single-work.php works, so I decided to do something different. I created some categories, under each category, there are sub-categories. I assigned work projects into these sub-categories. On work.php, I want to show the parent categories. When people click on it, they will see subcategories only for selected category. Then they click on subcategory, they will see a list of posts. I have got parent categories showing on work.php, code is below. But when I click on it, it is not showing sub-categories. How do I get this working?

<?php
$args = array(
    'child_of' => 31,
    'taxonomy' => 'kosher_category',
    'hide_empty' => 0,
    'hierarchical' => 1,
    'depth'  => 1,
    'parent' => 31
    );
$cats = get_categories( $args );
  foreach ($cats as $category) {
    $option = '<option value="/category/archives/'.$category->category_nicename.'">';
    $option .= $category->cat_name;
    $option .= '</option>';
    echo '<a href="' . get_category_link( $category->term_id ) . '">' .$option. '</a>';
  }

?> 

1 Answer

This StackOverflow question about how to show subcategories only may be useful to you, if you haven't seen it already.