How to List WordPress Posts by Category

If you want to loop through all your WordPress categories and then display the posts that belonged to that category below it, here’s how to do it. Just setup a new page with a custom page template and paste the code below:

/* template name: Posts by Category! */
get_header(); ?>
        <div id="container">
            <div id="content" role="main">
            // get all the categories from the database
            $cats = get_categories(); 
                // loop through the categries
                foreach ($cats as $cat) {
                    // setup the cateogory ID
                    $cat_id= $cat->term_id;
                    // Make a header for the cateogry
                    echo "<h2>".$cat->name."</h2>";
                    // create a custom wordpress query
                    // start the wordpress loop!
                    if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <?php // create our link now that the post is setup ?>
                        <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                        <?php echo '<hr/>'; ?>
                    <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
                <?php } // done the foreach statement ?>
            </div><!-- #content -->
        </div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

For more customization check out The Loop and query_posts().

Source | Demo