Skip to content

Instantly share code, notes, and snippets.

@krzysu
Last active December 11, 2015 22:58
Show Gist options
  • Save krzysu/4672792 to your computer and use it in GitHub Desktop.
Save krzysu/4672792 to your computer and use it in GitHub Desktop.
Wordpress: display last N posts on static page
<?php
$args = array( 'numberposts' => 2, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$recentposts = get_posts( $args );
if( empty( $recentposts )):
?>
<div class="three-from-four" id="latest-posts-box">
<?php
global $post;
foreach($recentposts as $index => $post):
setup_postdata( $post );
?>
<article class="one-from-two element <?= ($index % 2 == 1) ? 'last' : '' ?>">
<h2>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h2>
<div class="element-body">
<?php the_excerpt(); ?>
</div>
</article>
<?php
endforeach;
wp_reset_postdata();
?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment