petra5
Member
Posted 3 months ago #
I have a sidebar, where I want to display a list of subcategories of category 6 and every single subcategory would link to the subcategory template page. I also want every single subcategory to display a list of posts underneath - those that belong to this subcategory, but they should only be displayed when viewing subcategory template page or when viewing one of the posts belonging to that category. I currently have the code below, but I don't know how to continue. I am not sure how to make subcategories links. Any ideas? I also don't know how to display a list of posts under certain subcategory, only when viewing that subcategory page. Your help would be really appreciated.
[code moderated - please use the pastebin]
petra5
Member
Posted 3 months ago #
petra5
Member
Posted 3 months ago #
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.
petra5
Member
Posted 3 months ago #
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()) : ?>
petra5
Member
Posted 3 months ago #
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?
petra5
Member
Posted 3 months ago #
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>
petra5
Member
Posted 3 months ago #
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>