Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Created June 23, 2015 17:30
Show Gist options
  • Save igmoweb/7b4046f83a678f558baf to your computer and use it in GitHub Desktop.
Save igmoweb/7b4046f83a678f558baf to your computer and use it in GitHub Desktop.
posts-list-shortcode
<?php
add_shortcode( 'posts-list', 'ignacio_posts_list_shortcode' );
function ignacio_posts_list_shortcode( $atts ) {
$defaults = array(
'items' => 3
);
$atts = wp_parse_args( $atts, $defaults );
$args = array(
'ignore_sticky_posts' => true,
'posts_per_page' => $atts['items'],
'post_type' => 'post',
'post_status' => 'publish'
);
$posts = get_posts( $args );
$html = '<ol>';
foreach ( $posts as $post ) {
$html .= '<li style="border:1px solid black;margin-bottom:20px;">';
$html .= get_the_post_thumbnail( $post->ID, 'thumbnail' );
$html .= '<h2>' . get_the_title( $post->ID ) . '</h2>';
$html .= '</li>';
}
$html .= '</ol>';
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment