Skip to content

Instantly share code, notes, and snippets.

@derpixler
Created December 16, 2015 14:48
Show Gist options
  • Save derpixler/c39180155f81fc47045f to your computer and use it in GitHub Desktop.
Save derpixler/c39180155f81fc47045f to your computer and use it in GitHub Desktop.
Display WordPress latest post per shortcode
<?php
if ( ! function_exists( 'display_recent_posts' ) ) {
function display_recent_posts(){
$latest_posts = new WP_Query( 'posts_per_page=5' );
// The Loop
if ( $latest_posts->have_posts() ) :
while ( $latest_posts->have_posts() ) : $latest_posts->the_post();
$title = get_the_title();
$output .= <<<EOF
<h5>{$title}</h5>
EOF;
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
return $output;
}
}
add_shortcode( 'display_recent_posts', 'display_recent_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment