Thread Starter
petra5
(@petra5)
Thread Starter
petra5
(@petra5)
I tried this:
<?php $category_id = $category->term_id; ?>
<li><a href="<?php echo get_category_link($category_id); ?>"><?php echo $cat->name; ?></a></li>
but it is giving me a link to a top category (category 6) instead of relevant subcategory.
Thread Starter
petra5
(@petra5)
How do I get a category id of a sub category in a loop?
beginning of my code:
<?php $cats = get_categories('child_of=6');
foreach ($cats as $cat) :
$args = array(
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : ?>
Thread Starter
petra5
(@petra5)
Ok, I figured out how to get the categories links:
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></li>
Thanks alchymyth for pointing me in the right direction.
Now I just need to figure out how to display a list of posts under certain subcategory only when viewing that category. Any ideas where to start?
Thread Starter
petra5
(@petra5)
I currently have this loop to display posts:
<ul class="children">
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
Thread Starter
petra5
(@petra5)
Ok I’ve been trying different things, but no success just yet.
I have a list of subcategories in a sidebar 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?
Anyone?
the loop to show the posts:
<?php if ((in_category($thiscat)) || (is_category($thiscat))) { ?>
<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>