Mostrar posts recientes de una categoría determinada en WordPress

Si quieres mostrar posts recientes de una categoría determinada en WordPress mediante widgets puedes hacerlo usando el siguiente snippet en el archivo functions.php de tu tema:

function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'tucategoria', 'posts_per_page' => 10 ) ); 

// The Loop
if ( $the_query->have_posts() ) {
	$string .= '<ul class="postsbycategory widget_recent_entries">';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
			if ( has_post_thumbnail() ) {
			$string .= '<li>';
			$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
			} else { 
			// if no featured image is found
			$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
			}
			}
	} else {
	// no posts found
}
$string .= '</ul>';

return $string;

wp_reset_postdata();
}
// Añadir shortcode
add_shortcode('postscategoria', 'wpb_postsbycategory');

// Habilitar shortcode en widgets
add_filter('widget_text', 'do_shortcode');

Reemplaza «tucategoria» por la categoría que quieras mostrar y a continuación introduce un widget de texto y añade el shortcode [postscategoria]

Si quieres darle algo de estilo a la lista de posts prueba a introducir el siguiente código CSS o edítalo a tu gusto.

ul.postsbycategory {
list-style-type: none;
}

.postsbycategory img {
float:left; 
padding:3px;
margin:3px;
border: 3px solid #EEE;
}

Fuente: wpbeginner

Deja un comentario