I am trying to display all posts under a child child category. But my code keep rendering Parent category then all the post, then child and all the posts. I keep getting more categories the I want. This sidebar is used in single.php only.
My category structure looks like this.
Under Child-child I have posts, how I would like to render it.
<div id="child-child-title">
<ul>
<li class="post">title + post meta data</li>
<li class="post">title + post meta data</li>
<li class="post">title + post meta data</li>
</ul>
</div>
Here is what I am working with, and it is rendering every category (either if it's a parent or child) and every post on the site under it.
<?php global $post; $categories = get_the_category();foreach ($categories as $category) : ?>
<h4>» <?php echo $category->name; ?></h4>
<ul>
<?php
foreach((get_the_category()) as $category) {
$cat_ids=$cat_ids . $category->cat_ID . ',';
}
$cat_ids=substr($cat_ids, 0, -1);
//----------
?>
<?php $posts = get_posts('numberposts=20&category='. $cat_ids);foreach($posts as $post) : ?>
<a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li><br>
<?php endforeach; ?>
<li><strong><a href="<?php echo get_category_link($category->term_id);?>"><?php echo $category->name; ?> </a></strong></li>
<?php endforeach; ?>
</ul>
Thanks in advance!
Use query posts from a specific category slug
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
For more reference on using this visit Category Function reference
However, for subcategories (or child categories), 'The Category Name' doesn't always work. Rather use 'category-slug' instead.