Loop de WordPress para una categoría específica

12 feb 2013, 21:12:52
Vistas: 29.4K
Votos: 4

Tengo este fragmento de código para una categoría específica que funciona perfectamente, pero quiero hacer una pequeña modificación para mostrar las entradas en orden ascendente.

<?php

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

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

 <?php endwhile;

 // Resetear Consulta
 wp_reset_query();

 ?>
0
Todas las respuestas a la pregunta 3
0

Añade 'order'=> 'ASC' a tu consulta

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

Estoy bastante seguro de que query_posts es la peor manera de consultar...

Siempre usa get_posts, según lo que me dicen constantemente. Elimina los argumentos que no uses en el array a continuación.

$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 

Método WP_query con IDs de categoría:

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

O cambia la consulta así, pero no sé cómo añadir el orden 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' ); ¿quizás?
        return $query;
    }
12 feb 2013 22:11:54
0

Solo añade un argumento al array...

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

...pero no uses query_posts, mira aquí por qué. En su lugar podrías hacer esto:

$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