List Some Posts for Each Category

This is just a simple snippet to list the last posts you wrote for each blog category. Place it in your theme’s `functions.php` and call it anywhere via ``. It’ll generate a list of categories with links and classes like:

  • Category
    • Post 1
    • Post 2
    • Post 3

Code Snippet:

function so_cat_posts_menu() {

    global $post;
    $limit = 3;
    $cats = get_categories();

    <ul class="category-post-list">

        <?php foreach ($cats as $c) : ?>

                $posts = get_posts(array(
                    'numberposts' => $limit,
                    'category' => $c->term_id

            <?php if (count($posts) > 0) : ?>

                <li class="category-<?php echo $c->term_id; ?> category-<?php $c->name; ?>">
                    <a href="<?php echo get_category_link($c->term_id); ?>" title="<?php echo $c->category_nicename; ?>"><?php echo $c->category_nicename; ?></a>
                        <?php foreach ($posts as $post) : setup_postdata($post); ?>
                            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                        <?php endforeach; ?>

            <?php endif; ?>

        <?php endforeach; ?>



Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!