WordPress.org

Ready to get started?Download WordPress

Forums

Display one post per category (3 posts)

  1. mrdthomas
    Member
    Posted 4 years ago #

    I am trying to figure out a way to display 3 of the latest post from 8 categories. I need only show 1 post from each category so if cat1 has 3 post that are newer than the other categories I still only want to display one of them.

    Breakdown: Page that displays 3 of the latest post from 8 categories. Doesn't matter which category post is not displayed due to it being limited to displaying 3, just cant show more that 1 per category.

    I cant figure out logically how to do this. Any ideas?

    Thanks

  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. MichaelH
    Member
    Posted 4 years ago #

    Austin Matzko put this on the wp-hackers list some time back...
    -------------------------
    Latest post from each of six categories
    I would do something like this, where 1-6 are the category ids:

    $post_ids = array(0);
    foreach( array(1, 2, 3, 4, 5, 6) as $cat_id ) {
            if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 5)) ) {
                    $first = array_shift($posts);
                    $post_ids[] = $first->ID;
            }
    }
    query_posts(array('post__in' => $post_ids));

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.