List all posts by category / subcategory / sub-subcategory
-
I have categories setup like parent/ child/ grandchild:
Vehicles
–Airplanes
–Cars
—-Sports Cars
—-Economy Cars
–Trucks
–MotorcyclesI have the following code working… but it only outputs posts showing the immediate category, and because of that it’s not grouping by parent categories correctly – *should* look like this (with three posts existing)
Vehicles
–Airplanes
–Cars
—-Sports Cars
-PostTitle1
—-Economy Cars
-PostTitle2
–Trucks
–Motorcycles
-PostTitle3Instead, this is what it looks like:
Economy Cars
-PostTitle2
Motorcycles
-PostTitle3
Sports Cars
-PostTitle1Here’s the code I’m using:
<?php $cat_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'post_type' => 'company', 'orderby' => 'title', 'order' => 'ASC', 'showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 ); $posts=get_posts($args); if ($posts) { echo '<h2>' . $category->name . '</h2>'; echo '<ul>'; foreach($posts as $post) { setup_postdata($post); ?> <li> <h3><?php the_title(); ?></h3> </li> <?php } // foreach($posts echo '</ul>'; } // if ($posts } // foreach($categories ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘List all posts by category / subcategory / sub-subcategory’ is closed to new replies.