Loop WordPress pentru o categorie specifică

12 feb. 2013, 21:12:52
Vizualizări: 29.4K
Voturi: 4

Am acest fragment de cod pentru o categorie specifică care funcționează perfect, dar vreau o mică modificare pentru a afișa postările în ordine ascendentă.

<?php

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

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

 <?php endwhile;

 // Resetare Interogare
 wp_reset_query();

 ?>
0
Toate răspunsurile la întrebare 3
0

Adaugă 'order'=> 'ASC' la interogarea ta

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

Sunt sigur că query_posts este cea mai proastă metodă de a interoga...

Întotdeauna folosește get_posts, conform cu ce mi se spune constant. Elimină argumentele pe care nu le folosești din array-ul de mai jos.

$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('mai mult text');
          ?></a></div>
    <?php endforeach; ?>
<?php 

Metoda WP_query cu ID-uri de categorii:

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

Sau modifică interogarea astfel, dar nu știu cum să adaug ordinea crescătoare:

    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' ); poate?
        return $query;
    }
12 feb. 2013 22:11:54
0

Doar adaugă un argument în array...

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

...dar nu folosi query_posts, vezi aici de ce. În schimb, poți face asta:

$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