Loop di WordPress per una categoria specifica

12 feb 2013, 21:12:52
Visualizzazioni: 29.4K
Voti: 4

Ho questo snippet per una categoria specifica che funziona perfettamente, ma vorrei una piccola modifica per mostrare i post in ordine ascendente.

<?php

 // La Query
 query_posts( array ( 'category_name' => 'A', 'posts_per_page' => -1 ) );

 // Il Loop
while ( have_posts() ) : the_post(); ?>
   <li>
     <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
 </li>

 <?php endwhile;

 // Reset della Query
 wp_reset_query();

 ?>
0
Tutte le risposte alla domanda 3
0

Aggiungi 'order'=> 'ASC' alla tua query

 query_posts( array ( 'category_name' => 'A', 'order' => 'ASC', 'posts_per_page' => -1 ) );
12 feb 2013 21:19:03
0

Sono abbastanza sicuro che query_posts sia il modo peggiore per effettuare query...

Mi è stato detto ripetutamente di usare sempre get_posts. Rimuovi gli argomenti che non utilizzi nell'array qui sotto.

$args  = array(
    'posts_per_page'  => 5000,
    'offset'          => 0,
    'category'        => ,
    'orderby'         => 'post_date',
    'order'           => 'ASC',
    'include'         => ,
    'exclude'         => ,
    'meta_key'        => ,
    'meta_value'      => ,
    'post_type'       => 'post',
    'post_mime_type'  => ,
    'post_parent'     => ,
    'post_status'     => 'publish',
    'suppress_filters' => true ); 
$posts = get_posts($args);
    foreach ($posts as $post) :
    ?><div class="">
        <a href="<?php the_permalink();?>">
          <?php 
               echo the_title();
               echo the_post_thumbnail(array(360,360));
               the_excerpt('more text');
          ?></a></div>
    <?php endforeach; ?>
<?php 

Metodo WP_query con ID di categoria:

$query = new WP_Query( array( 'category__in' => array( 2, 6 ), 'order' => 'ASC') );

Oppure modifica la query così, ma non so come aggiungere l'ordine ascendente:

    add_action( 'pre_get_posts', 'add_my_custom_post_type' );

    function add_my_custom_post_type( $query ) {
        if ( $query->is_main_query() )
            $query->set( 'category', 'A' );
         // $query->set( 'order', 'ASC' ); forse?
        return $query;
    }
12 feb 2013 22:11:54
0

Aggiungi semplicemente un argomento all'array...

query_posts( array ( 'category_name' => 'A', 'posts_per_page' => -1, 'order' => 'ASC' ) );

...ma non usare query_posts, vedi qui perché. In alternativa potresti fare così:

$args = array(
    'category_name' => 'A',
    'posts_per_page' => -1,
    'order' => 'ASC',
);

$your_query = new WP_Query($args);

while ($your_query->have_posts()) : $your_query->the_post();
12 feb 2013 21:26:05