Last active
August 8, 2017 13:31
-
-
Save jmarreros/5780ec7560534773b171c06f528c1b99 to your computer and use it in GitHub Desktop.
Shortcut WordPress para mostrar las entradas recientes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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