igmoweb
6/23/2015 - 5:30 PM

posts-list-shortcode

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;
}