Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having trouble figuring out a correct loop to display the following:

Category Name:

  • Post 1
  • Post 2
  • Post 3
  • Post 4
  • Post 5

See all posts in this category -->

Can anyone help me with a correct function or loop?

share|improve this question
    
For a single category or for all? –  toscho Oct 26 '12 at 17:36
    
I think I want for all. So a page that would have a listing of categories like this. –  Destructo Oct 26 '12 at 17:52

1 Answer 1

$categories = get_categories();
foreach($categories as $c) {
    echo '<h2>'. $c->name . '</h2>';
    $posts = get_posts(array( 'category' => $c->term_id, 'numberposts' => 5 ));
    foreach($posts as $post) {
        echo $post->post_title;
    }
    ?>
    <a href="<?php echo get_category_link($c->term_id);?>">
        View all posts in <?php echo $c->name;?>
    </a>
    <?php
}
share|improve this answer
    
Thank so much! That worked great. –  Destructo Oct 29 '12 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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