Skip to content

Instantly share code, notes, and snippets.

@emaildano
Last active August 29, 2015 14:00
Show Gist options
  • Save emaildano/11182387 to your computer and use it in GitHub Desktop.
Save emaildano/11182387 to your computer and use it in GitHub Desktop.
Group blog posts by category boilerplate.
<?php
// 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; ?>
<h2><?php echo $cat->name ?></h2>
<?php
$args = array(
'cat' => $cat_id,
'posts_per_page' => 3
);
query_posts($args);
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('templates/post-module'); ?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment