如何显示category.php中的所有帖子,这些帖子按照添加帖子的日期以后代顺序排序

时间:2015-09-27 12:58:53

标签: php wordpress

我有一个包含bewlo代码的category.php文件。问题是,当向下滚动页面时,不是显示更新的帖子,而是以随机顺序显示帖子,而不是它们的完整列表!也许有人对可能出现的问题有一些想法?

 <?php get_header(); ?>
  <div class="content">
        <?php if ( have_posts() ) { ?>
      <div class="container">
              <h1>News</h1>
        <div class="news">
        <?php while ( have_posts() ) {
          the_post();
          if (has_post_thumbnail()) $thumbnail = '<div class="news_thumb">'.get_the_post_thumbnail($post->ID, 'medium').'</div>';
          else $thumbnail = '';
          add_filter( 'excerpt_length', 'custom_excerpt_length_author', 999 );
          $content = get_the_excerpt();
          remove_filter( 'excerpt_length', 'custom_excerpt_length_author', 999 );
          $len = (integer) mb_strlen(get_the_content(), 'UTF-8');
          if($post->post_content) $need_more = true;
          else $need_more = false;
        ?>
            <div class="news__item <?php echo ($need_more)?'news__item_more':''; ?>">
            <div class="news__item-inner">
              <?php echo $thumbnail; ?>
                        <span class="news__date"><?php the_time('d F Y'); ?></span>
                        <span class="news__title"><?php echo ($need_more)?'<a href="'.get_permalink().'">'.get_the_title().'</a>':get_the_title(); ?></span>
                        <div><?php echo $content; ?></div>
                    </div>
                </div>
            <?php }; ?>
        </div>
<?php
    $big = 999999999;
    $pages = paginate_links(array(
     'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
     'format' => '?paged=%#%',
     'prev_next' => true,
     'current' => max(1, get_query_var('paged') ),
     'total' => $wp_query->max_num_pages,
     'type' => 'list'
    ));
    if($pages){
     print '<div class="paging">'.$pages.'</div>';
    }
?>
              <div class="loader"><img src="<?php bloginfo( 'template_url' ); ?>/img/loader.gif" alt=""></div>
      </div>
        <?php }; ?>
  </div>
<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

我建议研究Wordpress Codex,特别是WP_Query类。

在您的情况下,您需要将循环更改为:

$args = array(
        'orderby'        => 'date', // Order posts by date added
        'order'          => 'DESC',  // Specify how to order - DESC for descending, ASC for ascending
        'posts_per_page' => -1 // To display all posts you need to give the value -1
       );

$query = new WP_Query( $args );

<?php if ( $query->have_posts() ) : ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <!-- your post content (title, thumbnail, excerpt, etc.) -->
<?php endwhile; ?>
<!-- end of the loop -->

<!-- pagination here -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

答案 1 :(得分:0)

我认为你可以像这样使用pre_get_posts钩子, 添加到您的functions.php

add_action('pre_get_posts', 'change_category_order', 10, 1); 
function change_category_order( $query ){ 
if( $query->is_category()){ 
    $query->set('order', 'desc');
}