Created
April 19, 2016 12:14
-
-
Save shadyvb/5b88abd77fdc475d8c5680cdc6cbfa80 to your computer and use it in GitHub Desktop.
Using a generator function for a simple post query loop
This file contains 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 | |
function get_latest_of_type( $post_type, $number ) { | |
$args = array( | |
'post_type' => $post_type, | |
'posts_per_page' => min( 10, $number ), | |
'no_found_rows' => true, | |
); | |
$query = new WP_Query( $args ); | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
yield $query->post; | |
} | |
wp_reset_query(); | |
} | |
?> | |
<?php foreach ( get_latest_of_type( 'job', 5 ) as $post ): ?> | |
<li> | |
<h5><?php the_title() ?></h5> | |
<div><?php the_excerpt(); ?></div> | |
</li> | |
<?php endforeach; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment