Quando usiamo più Loop nella stessa pagina, spesso sfruttando la funzione query_posts()
, possiamo avere come effetto indesiderato la duplicazione dei post già visualizzati. Questo avviene perchè i post vengono presi sempre nello stesso ordine e quindi la duplicazione è inevitabile. Tuttavia esiste una soluzione. Vediamo quale.
Nel nostro primo Loop, memorizziamo gli ID dei post in un array:
query_posts('showposts=8');
$ids = array();
while (have_posts()) : the_post();
$ids[] = get_the_ID();
the_title();
the_content();
endwhile;
Quindi usiamo questo array nel secondo Loop per escludere i post già visualizzati:
query_posts(array('post__not_in' => $ids));
while (have_posts()) : the_post();
the_title();
the_content();
endwhile;
Il parametro post__not_in
serve infatti ad escludere i post dal Loop.