最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress loop for specific category

programmeradmin1浏览0评论

i have this snippet for specific category it works perfect but i want little modification to show posts by Ascending order.

<?php

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

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

 <?php endwhile;

 // Reset Query
 wp_reset_query();

 ?>

i have this snippet for specific category it works perfect but i want little modification to show posts by Ascending order.

<?php

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

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

 <?php endwhile;

 // Reset Query
 wp_reset_query();

 ?>
Share Improve this question edited Feb 12, 2013 at 19:44 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 12, 2013 at 19:12 user23407user23407 431 gold badge1 silver badge3 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Pretty sure query_posts is the worst way to query...

Always use get_posts from what I'm constantly told. Remove the arguments that you don't use on the array below.

$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 

WP_query method with category id's:

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

Or change the query like this, but I don't know how to add ascending to it:

    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' ); maybe?
        return $query;
    }

Add 'order'=> 'ASC' to your query

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

Just add an argument to the array...

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

...but don't use query_posts, see here why. Instead you could do this:

$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();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论