Wordpress category sort by date

2019-09-07 01:10发布

问题:

I would like to sort my post in specyfic category ASC by date. My code is:

<?php $cat_id = 3; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 4, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
<?php the_post_thumbnail('thumbnail', array('class' => 'thumbnailmini')); ?>
<h3><?php the_title(); ?></h3>  
<?php the_content('więcej ->'); ?>
<div class="clear"></div>
<?php endwhile; endif; ?>

This code display 4 posts from category "3". How to sort this posts? I found:

<?php query_posts ('cat=3&orderby=date&order=ASC'); ?>

but it doesn't work.

Regards.

回答1:

try this,,

<?php $cat_id = 3; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 4,'orderby' => 'date','order' => 'ASC', 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
<?php the_post_thumbnail('thumbnail', array('class' => 'thumbnailmini')); ?>
<h3><?php the_title(); ?></h3>
<?php the_content('wiecej ->'); ?>
<div class="clear"></div>
<?php endwhile; endif; ?>