Skip to content

Instantly share code, notes, and snippets.

@bluvertigo
Last active September 29, 2017 07:25
Show Gist options
  • Select an option

  • Save bluvertigo/df68751210261dd54e25 to your computer and use it in GitHub Desktop.

Select an option

Save bluvertigo/df68751210261dd54e25 to your computer and use it in GitHub Desktop.
Shortcode - Restituisce la lista degli ultimi articoli
// Shortcode per visualizzare gli ultimi post
add_shortcode( 'list-posts', 'list_posts_shortcode' );
function list_posts_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'type' => 'post',
'order' => 'ASC',
'orderby' => 'date',
'posts_per_page' => -1
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts_per_page
);
$q = new WP_Query( $options );
$list = '<ul class="recent-posts">';
while($q->have_posts()) : $q->the_post();
$list .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>';
endwhile;
wp_reset_query();
return $list . '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment