Show one post from child categories
-
Building a theme from scratch, and I am trying to display info from one post from each child category under one main category (the main cat id is 18). So the structure is:
Cat A
Cat A1
Cat A2
Cat A3
I want to display information from one post each under Cat A1, Cat A2, and Cat A3 on a landing page. Then a user can click on the post info and to go to to the category, and from there click on a thumbnail to go to the actual post (the navigation is working).I have set up a custom post type called Property. The problem is that I just get one Property post appearing on the landing page–the latest post from Cat A1. No posts from Cat A2 or Cat A3 are appearing. If I remove ‘cat’ => 18 under the second array (which calls the Property posts), then I get the latest post from the entire list of Property posts, not from the category specified under the first array (which calls the child categories).
Fairly new to this so any help would be appreciated! My code is below.
Website: https://gmdevco.com/
Code:<!-- call each child category under cat 18 --> <?php $args = array( 'child_of' => 18, 'orderby' => 'date', 'order' => 'DESC', 'number' => 20 ); $categories = get_categories( $args ); foreach( $categories as $post ) setup_postdata( $post ); { ?> <!-- call one post from current child category --> <?php $args = array( 'post_type' => 'property', 'posts_per_page' => 1, 'orderby' => 'date', 'order'=> 'DESC', 'cat' => 18 ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
(Under this code, I have a div that displays the Property info, and that’s all working fine.)
- The topic ‘Show one post from child categories’ is closed to new replies.