How to show more than 5 posts?

I am running code that shows child categories, and all posts in the child categories. But if there are more than 5 posts in a category, only the 5 newest are shown.

How can I show all, or at least set a number like 9 posts etc.?

My code:

<?php
$args = array(
    'child_of' => 1
);
$categories = get_categories( $args );
foreach  ($categories as $category) {
    echo '<li><a>'.$category->name.'</a>';
    echo '<ul>';
    foreach (get_posts('cat=".$category->term_id) as $post) {
        setup_postdata( $post );
        echo "<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
    }
    echo '</ul></li>';
}
?>

3 Answers
3

I think you could use the posts_per_page argument in your get_posts query:

$args = array( 'child_of' => 1 );
$categories = get_categories( $args ); 
foreach ($categories as $category) {
    echo '<li><a>'.$category->name.'</a>';
    echo '<ul>';

    $posts_args = array(
        'posts_per_page' => 9,
        'category' => $category->term_id
    );
    foreach (get_posts($posts_args) as $post) {
        setup_postdata( $post );
        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';         
    }  
    echo '</ul></li>';
}

Leave a Comment