Skip to content

Instantly share code, notes, and snippets.

@dimaslanjaka
Created August 13, 2017 17:21
Show Gist options
  • Save dimaslanjaka/f4037ebe1543d8ec5953286e0086cfcc to your computer and use it in GitHub Desktop.
Save dimaslanjaka/f4037ebe1543d8ec5953286e0086cfcc to your computer and use it in GitHub Desktop.
// Add Shortcode
function recent_post_src( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'posts' => '5',
),
$atts,
'recent-posts'
);
// Query
$the_query = new WP_Query( array ( 'posts_per_page' => $atts['posts'] ) );
// Posts
$output = '<style>ul.custom {
list-style-type: none;
margin: 0;
padding: 0;
}</style>Recently Published:<br><ul class="custom">';
while ( $the_query->have_posts() ) :
$the_query->the_post();
$output .= '<li><a href="' . get_the_permalink() . '" rel="follow">' . get_the_title() . '</a></li>';
endwhile;
$output .= '</ul>';
// Reset post data
wp_reset_postdata();
// Return code
return $output;
}
add_shortcode( 'recent-posts', 'recent_post_src' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment