Molti temi di WordPress presentano tra i loro template di pagina un template specifico per mostrare una panoramica dei nostri post suddivisi in categorie o mostrati in ordine di pubblicazione. Qualora il nostro tema non disponesse di questa funzionalità , possiamo aggiungerla facilmente utilizzando due shortcode da inserire nella pagina. Vediamo come.
Il primo shortcode mostra un elenco di categorie. Aggiungete il seguente codice al file functions.php
del tema:
function show_posts() {
$args = array(
'show_last_update' => 1,
'show_count' => 1,
'title_li' => '',
'feed' => 'RSS'
);
wp_list_categories($args);
}
add_shortcode('posts', 'show_posts');
Questo shortcode mostra anche il numero di post pubblicati in ciascuna categoria e il relativo feed RSS. Il secondo shortcode, invece, mostra i trenta post più recenti con la data di pubblicazione e il numero di commenti:
function show_recent_posts() {
global $wpdb;
$args = array(
'posts_per_page' => 30,
'orderby' => 'date'
)
;
$loop = new WP_Query($args);
$html .= '<div id="recent-posts"><h3>Post recenti</h3>';
$html .= '<ul>';
while($loop->have_posts()) {
$loop->the_post();
$id = get_the_ID();
$comments = $wpdb->get_row("SELECT comment_count as count FROM $wpdb->posts WHERE ID = '$id'");
$comments_no = $comments->count;
$html .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a><small>' . get_the_date() . '</small><div>' .
$comments_no . ' comments</div></li>';
}
$html .= '</ul></div>';
echo $html;
}
add_shortcode('recent-posts', 'show_recent_posts');
Usando gli shortcode possiamo decidere se visualizzare l'uno o l'altro blocco di elementi o entrambi.