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

  1. lilblogger
    Posted 5 years ago #

    On the front page of my site: http://toronto.citylessons.com/, I want to list the 5 most recent posts for each parent category (excluding the "uncategorized" and "start-here" category).

    How do I do this?

  2. esmi
    Forum Moderator
    Posted 5 years ago #

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

  3. Michael
    Part-Time Forum Moderator
    Posted 5 years ago #

    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
    	 if (have_posts()) :
    	 echo $cat->name;
    	 while (have_posts()) : the_post(); ?>
    <!-- post output -->
    	<?php endwhile;
             else :
    	echo 'no posts for '.$cat->name;
    endforeach; ?>


  4. lilblogger
    Posted 5 years ago #

    @esmi - it would be instead of my most recent posts...

  5. esmi
    Forum Moderator
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.