WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. lilblogger
    Member
    Posted 3 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 3 years ago #

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

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 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
    );
    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

  4. lilblogger
    Member
    Posted 3 years ago #

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

  5. esmi
    Forum Moderator
    Posted 3 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

Tags

No tags yet.