Come mostrare post da diversi tipi di post in un singolo loop e visualizzarli separatamente nello stesso template
Ho quattro tipi di post
- Prodotti
- Banner
- Portfolio
- Testimonial
Tutti visualizzano i loro post sullo stesso template della home page (index.php).
Attualmente sto usando la seguente query per ottenere post da diversi tipi di post.
<?php
query_posts(
array(
'post_type' => 'work_projects',
'work_type' => 'website_development',
'posts_per_page' => 100
)
);
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_post_thumbnail($size);
the_title();
the_content();
endwhile; endif;
Il mio problema è che devo inserire più loop query_post
nella stessa pagina. Ho cercato soluzioni su Google e trovato risposte anche qui su Stack Overflow, ma non riesco a capire come usarle con the_title()
, the_content()
e the_post_thumbnail()
.

La migliore pratica sarebbe utilizzare quattro query personalizzate separate e ciclarle individualmente utilizzando wp_query
Ecco un esempio:
<?php
$custom_query = new WP_Query(
array(
'post_type' => 'work_projects',
'work_type' => 'website_development',
'posts_per_page' => 100
)
);
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
the_post_thumbnail($size);
the_title();
the_content();
endwhile; endif; wp_reset_query();
?>
Poi ripeteresti il processo per ogni tipo di post, sostituendo 'work_projects' con il nome del prossimo tipo di post.
Non sono sicuro di cosa sia 'work_type' in questo scenario, ma l'ho semplicemente reinserito nella query visto che era già presente.
