• 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: http://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.)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    If I understand it right, you’re getting the child categories with the outer loop and one latest post with the inner loop.

    The problem with the second loop is that you’re getting a post category 18 instead of using the child categories you got from the outer loop.

    I haven’t test your code, but the inner loop should look something like this:

    $args = array( 'post_type' => 'property', 'posts_per_page' => 1, 'orderby' => 'date', 'order'=> 'DESC', 'cat' => $post->term_id );

    $post refers to foreach( $categories as $post ) from the outer loop.

    More info: http://codex.wordpress.org/Function_Reference/get_categories

    Thread Starter anjaseligman

    (@anjaseligman)

    Thank you for your response, and sorry for my delayed response–I never got an email that there was a reply!

    You are correct–the outer loop calls all of the child categories of cat 18, and the inner loop should display one post from each of those child cats. I tried your code edits, and I am still getting the same result. I think the problem must be in the outer loop, since it seems like just one category is being called?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show one post from child categories’ is closed to new replies.