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>';
}
?>

Solutions Collecting From Web of "How to show more than 5 posts?"

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>';
}

You’ve to make use of the post_per_page or numberposts parameter of get_posts(). The parameter defaults to 5, see source, just chose the value you actually want it to have in your get_posts() call and the default value will be overridden.

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it’s -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
        <?php
    }
    ?>
    </ul>
    <?php
}
?>