Featured posts senza alcun plugin

Se siete interessati a non far finire nel dimenticatoio i vostri post più gloriosi e volete creare una sezione del blog oppure della sidebar con la segnalazione di uno di questi post, potete installare il plugin specifico Random featured post oppure procedere utilizzando gli strumenti che WordPress vi mette a disposizione.

Fino a poco tempo fa utilizzavo, con soddisfazione, il plugin. L’unico limite, se così può dirsi, di quel plugin è che potete far visualizzare solo categorie (e scegliere all’interno di esse quale visualizzare) e non tag. Col tempo, però, e con l’utilizzo di WordPress, le mie esigenze sono cambiate: avevo bisogno di far visualizzare i post di un determinato tag (quello Featured) e non quelli di una categoria.

Featured post per tag

In questi due esempi, che sono alternativi (o usate l’uno o l’altro), ho utilizzato il tag featured (tag=featured). Voi, ovviamente, potete inserire il tag che preferite. Ho poi scelto di far visualizzare un solo post (posts_per_page=1) e ho detto a WordPress di sceglierlo a caso (orderby=rand). Per ulteriori informazioni seguite il link che riporto a fine articolo.

Featured post per tag con WP_Query

<h3>Featured post</h3>
<?php $recent = new WP_Query('tag=featured&posts_per_page=1&orderby=rand'); while($recent->have_posts()) : $recent->the_post();?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Link a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Featured post per tag con query_posts

Potete anche utilizzare questa forma:

<h3>Featured post</h3>
<?php query_posts('tag=featured&posts_per_page=1&orderby=rand'); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Link a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Featured post per categoria

Se invece optate per visualizzare i post di una categoria, inserite al posto della mia 172 (cat=172) la ID della categoria che volete usare. L’ID la trovate in Bacheca / Gestione / Categorie. Nell’elenco sottostante fermate il mouse sopra la categoria che vi interessa e osservate il link nella barra di stato: il numero finale del link è il numero della categoria.

Featured post per categoria con WP_Query

<h3>Featured post</h3>
<?php $recent = new WP_Query('cat=172&posts_per_page=1&orderby=rand'); while($recent->have_posts()) : $recent->the_post();?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Link a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Featured post per categoria con query_posts

Oppure questa forma:

<h3>Featured post</h3>
<?php query_posts('cat=172&posts_per_page=1&orderby=rand'); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Link a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Ulteriori informazioni

Per ulteriori informazioni sulle due funzioni WP_Query e query_posts, visitate le rispettive pagine su codex.wordpress.org

Fonte: Stylizedweb.com

Photo courtesy: DavidDennisPhotos.com, Costa Rican Bookmarks, CC by-sa.