Come mostrare post da diversi tipi di post in un singolo loop e visualizzarli separatamente nello stesso template

14 feb 2017, 16:02:59
Visualizzazioni: 13.8K
Voti: 1

Ho quattro tipi di post

  1. Prodotti
  2. Banner
  3. Portfolio
  4. 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().

0
Tutte le risposte alla domanda 2
0

Il parametro post_type accetta un array. Hai provato a usare WP_Query con un array?

$args = array( 
         'post_type' => array( 'product', 'banner', 'portfolio', 'testimonial'),
         'post_status' => 'publish',
         'posts_per_page' => 100
    );
    $the_query = new WP_Query( $args );
14 feb 2017 18:08:16
0

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.

14 feb 2017 16:30:16