Created
December 16, 2015 14:48
-
-
Save derpixler/c39180155f81fc47045f to your computer and use it in GitHub Desktop.
Display WordPress latest post per shortcode
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 | |
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