简体   繁体   中英

How to display all posts in category.php sorted in descendent order by the date the post was added

I have a category.php file with the code bewlo. The problem is that instead of the more recent posts to be shown when scrolling down the page, the posts are shown in the random order, and also not the full list of them! Maybe someone has some ideas of what might be wrong?

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

I would suggest studying the Wordpress Codex , specifically the WP_Query class.

In your case, you would need to change your loop to :

$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; ?>

I think you can use the pre_get_posts hook like this,, add to your 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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM