• Resolved milet22

    (@milet22)


    I’ve searched the web and forum so I think this is unanswered.

    On the homepage I need to display a certain number of posts from specific categories. For example, the home page should have the X most recent posts from category A, the Y most recent posts from category B, and the Z most recent posts from category C.

    Is this possible? If there’s a plugin, awesome; otherwise I’m willing to modify code as necessary, but I’m completely unfamiliar with WP code.

    Can anyone help?

Viewing 4 replies - 1 through 4 (of 4 total)
  • A starting place of code for you is here
    https://www.nabble.com/Re%3A-Pull-Latest-Post-from-Each-of-6-Categories-p23380122.html

    That code gets one post per category. A simple addition will let you specify a specific # of posts per category

    Thread Starter milet22

    (@milet22)

    stvwlf, thanks for code suggestion–it is a good place to start indeed.

    I know this is a beginner question, but where does that go in the index.php file and does it replace something already in there?

    Thanks!

    Hi

    That is a good question, and I should have provided the answer. Put it before the WordPress loop. The loop starts with this code

    <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>

    Thread Starter milet22

    (@milet22)

    Thanks so much for your help, stvwlf!

    In case anyone needs to do this, I modified the code from here for multiple posts.

    $post_limit_per_cat = 5;
    
    $post_ids = array(0);
    foreach( array(7) as $cat_id ) {
            if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => $post_limit_per_cat)) ) {
    		$post_current_count = 0;
    
                    while( (count($posts) > 0) AND ($post_current_count < $post_limit_per_cat) ) {
    	                $first = array_shift($posts);
    	                $post_ids[] = $first->ID;
    	                $post_current_count++;
                    }
            }
    }
    query_posts(array('post__in' => $post_ids));
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Certain Number of Posts from Multiple Categories’ is closed to new replies.