2

我正在使用以下内容,但我的帖子仍按时间顺序排列(从旧到新)。我的目标是在顶部发布最新帖子。(从新到旧)

$catquery = new WP_Query( array (
    'cat'=>'27', 
    'post_type'=>'news', 
    'orderby' => "post_date", 
    'order' => "DESC" ) 
);

while($catquery->have_posts()) : $catquery->the_post();

<p class="date"> <?php the_date(); ?> </p>

<h3><?php the_title(); ?></h3>

<p> <?php the_content('Read More', FALSE); ?> 

我也尝试过orderby' => "date",但没有运气。如何解决这个问题?

4

2 回答 2

1
    your post is custom post type so use this argument:'
    <?php 
    $args = array(
        'tax_query' => array(
                array(

                'taxonomy' => 'news_category',

                'field' => 'id',

                'terms' => '27'

            )
        ),
        'post_type'=>'news',
        'order_by'=>'date',
        'order'=>'DESC',
        'posts_per_page'=>-1
);
    query_posts($args);
    while ( have_posts() ) : the_post(); 
?>
    <li>
        <p class="date"><?php echo get_the_date('F j, Y'); ?></p>
        <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    </li> 
<?php 
endwhile;
wp_reset_query();
?>
于 2013-09-10T12:50:52.350 回答
1

您的代码很接近,但存在一些问题。

  1. 'cat'期望一个 int 不是一个字符串,所以你需要'cat'=>27,
  2. 而不是post_date你需要date
  3. 我不确定您需要哪个订单,所以ASC如果DESC不起作用,请尝试。

这是新的查询:

$catquery = new WP_Query(array (
  'cat'       => 27, 
  'post_type' => 'news', 
  'orderby'   => 'date', 
  'order'     => 'DESC'
));
  • order (string) - 指定 'orderby' 参数的升序或降序。默认为“DESC”。
    • 'ASC' - 从最低到最高值的升序(1、2、3;a、b、c)。
    • 'DESC' - 从最高值到最低值的降序(3、2、1;c、b、a)。

这是一个参考:WP_Query

于 2013-09-10T12:15:58.323 回答