Last active
August 4, 2018 18:02
-
-
Save calvez/8bf407d370373d05c86b64225d27f340 to your computer and use it in GitHub Desktop.
How To Create WordPress Recent Posts Shortcode
This file contains hidden or 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
//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