显示从最近的帖子到Wordpress中最早的帖子

时间:2018-10-09 07:00:57

标签: wordpress

我想在顶部显示最近发布的帖子,并对显示的发布顺序进行排序(从最新到最早)。

我有以下代码。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php
   <div class="blog-posts">
     <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 pre-post-wrapper">
      <article data-aos="zoom-in-up" data-aos-offset="100" class="blog-post-wrapper">


        <div class="post-img">
          <?php $thumb = null; ?>
          <?php if (has_post_thumbnail()): ?>
              <?php $thumb = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
              <a href="<?php echo get_permalink(); ?>"><img src="<?php echo $thumb; ?>" alt=""></a>
          <?php else: ?>
              <a href="<?php echo get_permalink(); ?>"><img src="<?php echo get_template_directory_uri() ?>/framework/assets/images/default.jpg" alt=""></a>
          <?php endif; ?>
          <div class="bp-img-arrow">
            <i class="fa fa-arrow-right" aria-hidden="true"></i>
          </div>
        </div>
        <div class="post-content">
            <h2 class="post-title"><a href="<?php echo get_permalink() ?>"><?php echo wp_trim_words(get_the_title(), 14); ?></a></h2>
            <p><?php echo wp_trim_words(apply_filters('the_content', get_the_content()), 45); ?></p>
            <a href="<?php echo get_permalink() ?>" class="post-link">Read more <i class="fa fa-angle-double-right"></i></a>
            <div class="clearfix"></div>

            <div class="bp-details">
              <div class="bp-comment">
                <?php $c_count =get_comments_number(); ?>
                <span><i class="fa fa-comment-o" aria-hidden="true"></i><?php echo $c_count ?> Comments</span>
              </div>
              <div class="bp-time">
                <?php if(get_field('read_time')){
                        $read_time = get_field('read_time').'Minutes';?>
                        <span><i class="fa fa-clock-o" aria-hidden="true"></i><?php echo $read_time ?></span>
                      <?php
                       }else{
                        // $read_time = 'Not Defined';
                      } ?>

              </div>
            </div>
        </div>
    </article>
  </div>
 </div>
?>

<?php endwhile; ?>

<div class="pagination-wrapper">
   <?php pagination(); ?>
</div>

<?php else: ?>
   <h5>No Posts found.</h5>
<?php endif; ?>

此代码显示了所有帖子,但问题在于它以某种方式使帖子混乱。在顶部和底部都看不到最新的帖子。在中间的某个地方。如何对帖子的显示顺序进行排序?

2 个答案:

答案 0 :(得分:0)

在Wordpress Codex中,您可以找到所有的想法兄弟:

    <?php 
    // the query
    $all_posts = new WP_Query(array('post_type'=>'post', 
    'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
    <?php if ( $all_posts->have_posts() ) : ?>
    <ul>
    <!-- the loop -->
    <?php while ( $all_posts->have_posts() ) : $all_posts->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <!-- end of the loop -->
   </ul>
    <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

答案 1 :(得分:0)

<?php
   $query = new WP_Query(
      array(
         'post_type'=>'post',
         'post_status' => 'publish',
         'orderby' => 'publish_date',
         'order' => 'DESC',
         'posts_per_page' => -1

      )
   );
?>

希望,这会有所帮助。谢谢。