1

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

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

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
}
?>
3
  • Looks good, and ready made is far easier to understand & implement! But I think there are some issues with the code? No close of the if or while? The 2nd last ?> was not opened?
    – Jon
    Feb 25 '14 at 16:49
  • You're right, I forgot one <?php tag. The rest, however, is fine. What do you mean by No close of the if or while? For each there is a code block built by { ... }. And BTW, you should use wp_reset_postdata no matter with which solution you end up.
    – tfrommen
    Feb 25 '14 at 16:51
  • I got it working thanks, and I prefer this code than to echo the whole html method. But I had to remove the if function as it did not work with it. And you had added a <ul> which I had outside the code. I had been using endif & endwhile but I guess it is not needed here.
    – Jon
    Feb 25 '14 at 17:18
2

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.

2
  • I really should learn to be brief. ;) +1
    – tfrommen
    Feb 25 '14 at 15:25
  • Haha, thanks your answer on the other hand is ready-made. @tf
    – Nicolai
    Feb 25 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.