Skip to content

Instantly share code, notes, and snippets.

@chocopie
Last active March 2, 2020 13:38
Show Gist options
  • Save chocopie/c76704444f4f18ad9deae87544ca81fd to your computer and use it in GitHub Desktop.
Save chocopie/c76704444f4f18ad9deae87544ca81fd to your computer and use it in GitHub Desktop.
Shortcode for showing latest post on a page (Wordpress)
function getLatestPostHomepage() {
global $post;
$args = array( 'numberposts' => 1 );
$latestposts = get_posts($args);
foreach ($latestposts as $post) {
setup_postdata($post);
echo the_title("<h2>","</h2>");
echo the_content();
}
wp_reset_postdata();
}
add_shortcode('LatestPostHomepage', 'getLatestPostHomepage');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment