WP_Query mostra solo UN post, non funziona

27 giu 2012, 20:42:30
Visualizzazioni: 22.9K
Voti: 3

Non importa cosa faccia, NON riesco a capire perché WP_Query estrae tutti i post e ignora completamente posts_per_page ... Voglio mostrare solo un post.

<?php
// Il problema è che posts_per_page è impostato a -1, che mostra TUTTI i post
$args = array(
'post_type'      => 'post',
'orderby'        => 'date',
'order'          => 'ASC',
'posts_per_page' => -1  // Cambiare questo valore in 1 per mostrare un solo post
);

// La Query
$the_query = new WP_Query( $args );

// Il Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>

<div style="padding: 15px;">
    <div class="grid_4 alpha">immagine</div>
    <div class="grid_7 omega">
        <?php the_excerpt(); ?>
    </div>
</div>
<div style="clear: both;"></div>
<div style="height: 33px; background-color: #5ba4d8; position: relative;">
    <div style="width: 300px; line-height: 33px;"><?php the_title(); ?></div>
    <a href="" style="position: absolute; right: 0; top: 0;"><span class="nav-blog-next"></span></a>
</div>

<?php   
endwhile;

// Reimposta i Dati del Post
wp_reset_postdata();
?>
0
Tutte le risposte alla domanda 2
0

Cambia posts_per_page a 1 solamente, non -1.

In questo modo: 'posts_per_page' => 1

-1 significa tutti.

27 giu 2012 20:52:25
0

Dal titolo della tua domanda, sembra che tu stia cercando di recuperare un singolo post. Il problema è che hai passato -1 a posts_per_page, il che equivale a dire alla query di recuperare tutti i post.

Usa invece:

'posts_per_page' => 1
27 giu 2012 20:53:04