0

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?

2
  • For a single category or for all?
    – fuxia
    Commented Oct 26, 2012 at 17:36
  • I think I want for all. So a page that would have a listing of categories like this.
    – Destructo
    Commented Oct 26, 2012 at 17:52

1 Answer 1

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
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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