Skip to content

Instantly share code, notes, and snippets.

@mchavezi
Created April 8, 2015 23:36
Show Gist options
  • Select an option

  • Save mchavezi/d0da516fd6b6fe910744 to your computer and use it in GitHub Desktop.

Select an option

Save mchavezi/d0da516fd6b6fe910744 to your computer and use it in GitHub Desktop.
One Post From Each Category
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment