Post correlati su WordPress senza plugin! (parte 2)
Nel post precedente ho pubblicato una funzione per wordpress che restituiva una lista di articoli correlati in base alla categoria. Come avevo accennato si può anche ottenere la lista in base ai meta tag del post.
La funzione è molto simile alla precedente.
function relatedPost($postObj, $postNumber = 4){
if ($tags = wp_get_post_tags($postObj->ID))
{
$tagIds = array();
foreach($tags as $tag)
{
$tagIds[] = $tag->term_id;
}
$query = new wp_query(array(
'category__in' => $tagIds,
'post__not_in' => array($postObj->ID),
'posts_per_page'=> $postNumber,
'caller_get_posts'=> 1
));
if($query->have_posts())
{
while($query->have_posts())
{
$query->the_post();
echo '<li><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
}
}
}
wp_reset_query();
}
Cambia giusto la parte iniziale, invece che ottenere la lista delle categorie prende la lista delle tag e poi come la funzione precedente esegue una query che restituisce i post.
Nel caso volessimo ottenere anche l’immagine di anteprima del post, se il template lo consente si può utilizzare la funzione the_post_thumbnail.