Created
April 8, 2015 23:36
-
-
Save mchavezi/d0da516fd6b6fe910744 to your computer and use it in GitHub Desktop.
One Post From Each Category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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">→</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