• Resolved gregcrowe

    (@gregcrowe)


    Hi, I have the following category structure: Reviews {empty parent} with children Game Review, Time Waster, and Book Review. I am using get_posts to pull the most recent posts from any of the children with the following code in a unique template:

    <?php
        $args = array( 'posts_per_page' => 3, 'category' => 2 ); //the ID of Reviews.
        $reviews = get_posts( $args );
        foreach ( $reviews as $post ) : setup_postdata( $post );
    ?>
        {output title, abstract, etc.}
    <?php
        endforeach;
        wp_reset_postdata();
    ?>

    And that all works fine. However, after this listing I would like to have a separate list in each individual child category offset by however many in that particular category showed up in the top-3 list. Unfortunately, whenever I try to find out the categories of each of the three posts using the_category, it always gives me ‘2’ the ID of the parent.

    Is there an easier way to get the actual category of a post that were gotten with its parent category?

Viewing 1 replies (of 1 total)
  • Thread Starter gregcrowe

    (@gregcrowe)

    I don’t know why I couldn’t get this to work before, but this time I got it. For reference, I added the following inside the foreach loop above:

    foreach((get_the_category()) as $category) {
        if ($category->cat_ID == 1683) {
            ++$gamecount;
        } elseif ($category->cat_ID == 8) {
            ++$twcount;
        } elseif ($category->cat_ID == 1589) {
            ++$bookcount;
        }
    }

    I then used those variables as the offsets for the child-specific get_posts queries.

Viewing 1 replies (of 1 total)

The topic ‘Identify category when get_posts pulls from parent’ is closed to new replies.