WordPress.org

Support

Support » Themes and Templates » Help with PHP to show 5 recent posts from each category?

Help with PHP to show 5 recent posts from each category?

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi

    @esmi

    Forum Moderator

    Would this be instead of your recent posts display or as well as?

    Moderator Michael

    @alchymyth

    Forum Moderator

    rough structure, unformatted, untested (for the exclude, use cat ID), for example:

    <?php  $cats = get_categories('exclude=1,23');
    foreach ($cats as $cat) :
    if(get_categories('child_of='.$cat->term_id)) continue;
    $args = array(
    'posts_per_page' =>5, // max number of post per category
    'cat' => $cat->term_id
    );
    query_posts($args); 
    
    	 if (have_posts()) :
    	 echo $cat->name;
    	 while (have_posts()) : the_post(); ?>
    
    <!-- post output -->
    
    	<?php endwhile;
             else :
    	echo 'no posts for '.$cat->name;
    	endif;
    	wp_reset_query(); 
    
    endforeach; ?>

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

    lilblogger

    @lilblogger

    @esmi – it would be instead of my most recent posts…

    esmi

    @esmi

    Forum Moderator

    alchymyth’s code should work if you add it to your theme’s index.php template file in place of your your current Loop output/display. You’d just have to replace the numbers in <?php $cats = get_categories('exclude=1,23'); with the ids of your “uncategorized” and “start-here” categories.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help with PHP to show 5 recent posts from each category?’ is closed to new replies.