Come ottenere i commenti per ID del post?

12 gen 2012, 23:08:09
Visualizzazioni: 34.1K
Voti: 9

Ho questa query personalizzata per elencare tutti i post all'interno di una categoria specifica. Per esempio ho questo:

$args = array('cat' => 'home','post_type' => 'post'));
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
 // esegui operazioni qui
endwhile;

Quindi per questa pagina vorrei mostrare l'elenco dei post ma anche i relativi commenti. Sto mostrando solo un massimo di 2 commenti per ogni post.

Esiste una funzione integrata per fare questo?

0
Tutte le risposte alla domanda 1
0
10

Puoi usare get_comments. Riferimento Funzioni/get comments

$args = array('cat' => 'home','post_type' => 'post'));
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
    //visualizza commenti
    $comments = get_comments(array(
        'post_id' => $post->ID,
        'number' => '2' ));
    foreach($comments as $comment) {
        //formatta commenti
    }
endwhile;
12 gen 2012 23:25:19