简体   繁体   English

如何显示category.php中的所有帖子,并按帖子添加的日期按降序排列

[英]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. 我有一个带有代码bewlo的category.php文件。 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. 我建议研究Wordpress Codex ,特别是WP_Query类。

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 我认为您可以像这样使用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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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