Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Last active August 8, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmarreros/5780ec7560534773b171c06f528c1b99 to your computer and use it in GitHub Desktop.
Save jmarreros/5780ec7560534773b171c06f528c1b99 to your computer and use it in GitHub Desktop.
Shortcut WordPress para mostrar las entradas recientes.
<?php
add_action( 'init', 'dcms_agregar_shortcode' );
function dcms_agregar_shortcode(){
add_shortcode('EntradasRecientes', 'dcms_entradasrecientes');
}
function dcms_entradasrecientes( $atts , $content ){
//Valores por defecto
$atts = shortcode_atts(['cantidad' => '4'], $atts, 'EntradasRecientes' );
$quantity = (int) $atts['cantidad'];
$title = ($content) ? $content : 'Entradas Recientes';
$str = '';
//Consulta entradas
query_posts(['orderby' => 'date', 'order' => 'DESC' , 'showposts' => $quantity]);
if ( have_posts() ) {
$str = '<h3>'.$title.'</h3>';
$str .= '<ul>';
while ( have_posts()) {
the_post();
$str .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
$str .= '</ul>';
}
wp_reset_query();
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment