I have a list of subcategories in a sidebar (children of top level category) and I also get a list of posts underneath each category. I only want to show those posts when we are under that subcategory or when we are viewing one of the posts. Each post comes under top level category and subcategory and I don't know how to get current subcategory id/name. I understand you get a category ID like this: $category = get_the_category(); $catID = $category[0]->cat_ID; but what about when a post belongs to subcategory and category and I just need a subcategory id I believe? Current code:
<?php $cats = get_categories('child_of=6');
$this_category = get_category($cat);
//echo $parent_category;
foreach ($cats as $cat) :
$args = array(
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : ?>
<li><a href="<?php echo get_category_link($cat); ?>"><?php echo $cat->name; ?></a>
<?php if ($this_category->category_parent != 0) { ?>
<ul class="children">
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php /*general loop output; for instance: */ ?>
<li>- <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php } ?>
</li>
<?php else :
echo 'No Posts for '.$cat->name;
endif;