Skip to content

Instantly share code, notes, and snippets.

@calvez
Last active August 4, 2018 18:02
Show Gist options
  • Save calvez/8bf407d370373d05c86b64225d27f340 to your computer and use it in GitHub Desktop.
Save calvez/8bf407d370373d05c86b64225d27f340 to your computer and use it in GitHub Desktop.
How To Create WordPress Recent Posts Shortcode
//https://chiefthemes.com/create-wordpress-recent-posts-shortcode/
<?php
function chiefthemes_posts_shortcode($atts, $content = NULL)
{
$atts = shortcode_atts(
[
'orderby' => 'date',
'posts_per_page' => '10'
], $atts, 'recent-posts' );
$query = new WP_Query( $atts );
$output = '<ul class="recent-posts">';
while($query->have_posts()) : $query->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a> - <small>' . get_the_date() . '</small></li>';
endwhile;
wp_reset_query();
return $output . '</ul>';
}
add_shortcode('recent-posts', 'chiefthemes_posts_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment