Se in WordPress si programmano dei post per la pubblicazione futura potrebbe essere utile mostrare ai lettori il titolo dei prossimi post e la loro data di pubblicazione, in modo da creare interesse ed attesa per i nostri aggiornamenti. Possiamo creare un semplice shortcode che può essere inserito anche in un widget di testo posto nella colonna laterale. Vediamo come fare.
Aggiungete il seguente codice al vostro file functions.php
del tema:
function show_scheduled_posts() {
$query = new WP_Query(array('showposts'=>3, 'post_status'=>'future', order=>'ASC'));
$html = '';
if($query->have_posts()) {
$html .= '<div id="upcoming-posts">';
$html .= '<ul>';
while($query->have_posts()) {
$query->the_post();
$html .= '<li><strong>' . get_the_title() . '</strong><span>' .
get_the_date() . '</span></li>';
}
$html .= '</ul></div>';
} else {
$html .= '<p>Nessun post programmato.</p>';
}
return $html;
}
add_shortcode('scheduled', 'show_scheduled_posts');
A questo punto abbiamo creato lo shortcode scheduled
che mostra i nostri prossimi tre post. Se volete aumentare il numero di post è sufficiente modificare il valore del parametro showposts
della classe WP_Query
.