简体   繁体   English

WordPress-按日期获取帖子

[英]WordPress - Get Posts by Date

I am trying to figure out how to construct a WordPress query that is going to get posts by date. 我试图弄清楚如何构造一个WordPress查询,该查询将按日期获取帖子。

What I mean by this, is that I'd like a way to output my WordPress posts something like this: 我的意思是,我想要一种输出类似这样的WordPress帖子的方法:

<div class="day-posts">
     <div class="day">
          <div class="title">Monday 1st January 2014</div>
          <div class="posts">
               <div class="post">
                    <div class="title">Post for this day</div>
                    <div class="content">Lorum ipsum dolar sit amet</div>
               </div>
          </div>
     </div>
     <div class="day">
          <div class="title">Tuesday 2nd January 2014</div>
          <div class="posts">
               <div class="post">
                    <div class="title">Post for this day</div>
                    <div class="content">Lorum ipsum dolar sit amet</div>
               </div>
          </div>
     </div>
</div>

This needs to work with posts that are not published yet also, using the "post_status=future" query option. 这需要使用“ post_status = future”查询选项来处理尚未发布的帖子。

I have spent about an hour searching online to see if someone has done something like this before, but unfortunatly I haven't found anything viable. 我花了大约一个小时在网上搜索,看看以前是否有人做过这样的事情,但是不幸的是,我还没有发现任何可行的方法。

I have also made some attempts at this myself but unfortunatly, I haven't been able to come up with anything that works. 我本人也对此进行了一些尝试,但不幸的是,我无法提出任何可行的方法。

Does anyone have some suggestions or insights on how this might be done? 有没有人对如何做到这一点有任何建议或见解?

Thanks 谢谢

<?php
$query = new WP_Query(array('orderby' => 'post_date', 'order' => 'DESC')); //Orders posts by post_date in descending order

// The Loop
if ($query->have_posts()) {
    echo '<div class="day-posts">';
    while ($query->have_posts()) {
        $query->the_post();
        echo '<div class="day">';
        the_date('l jS F Y', '<div class="title">', '</div>'); //Formats date, before echo, after echo
        echo '<div class="posts">';
        echo '<div class="post">';
        the_title('<div class="title">', '</div>'); //echo title
        echo '<div class="content">';
        the_content(); //echo content
        echo '</div></div></div></div>';
    }
    echo '</div>';
}
?>

I put this together in a few minutes, sorry I didn't concatenate and make it all nice, but you can do that. 我在几分钟内将它们放在一起,很抱歉,我没有将其连接起来并使其变得很好,但是您可以这样做。

Although at first glance it looks like a simple task, it is tricky to build a nested loop with a single query. 尽管乍看之下看起来像是一个简单的任务,但是使用单个查询构建嵌套循环却很棘手。

First step is to build a query, for the demonstration I will take all posts ( posts_per_page=-1 ) in ascending order ( order=asc ) with any post status ( post_status=any ). 第一步是构建查询,在演示中,我将按升序( order=asc )接收所有帖子状态( post_status=any )的所有帖子( posts_per_page=-1 )。 Post status can be changed by using array of arguments like 'post_status' => array( 'publish', 'future' ) ( documentation ). 可以通过使用诸如'post_status' => array( 'publish', 'future' )的参数'post_status' => array( 'publish', 'future' )文档 )来更改发布状态。

Next step is to build multidimensional array of posts: 下一步是构建帖子的多维数组:

$posts_by_day = array_reduce( $query->posts, function( $r, $v ) {
    $r[ date( 'Y-m-d', strtotime( $v->post_date ) ) ][] = $v;
    return $r;  
});

An array that looks like: 看起来像这样的数组:

Array
    (
        [2014-03-17] => Array
            (
                [0] => WP_Post Object ( ... )
                [1] => WP_Post Object ( ... )
        )
        [2014-03-24] => Array
            ( 
                [0] => WP_Post Object ( ... )
                [1] => WP_Post Object ( ... )
                [2] => WP_Post Object ( ... )
        )
        ...
    )

And finally loop through an array, by date in the outer loop, and by posts on that date in the inner loop. 最后,通过数组循环,按外部循环中的日期,按内部循环中该日期的帖子。 setup postdata() is used to set up a global post data so that Template Tags can work in the current post context. setup postdata()用于设置全局发布数据,以便模板标签可以在当前发布上下文中使用。

<?php
    $query = new wp_query( 'post_status=any&order=asc&posts_per_page=-1' );

    $posts_by_day = array_reduce( $query->posts, function( $r, $v ) {
        $r[ date( 'Y-m-d', strtotime( $v->post_date ) ) ][] = $v;
        return $r;  
    });
?>

<?php if ( $posts_by_day ) : ?>
    <div class="day-posts">
    <?php foreach( $posts_by_day as $day => $day_posts ) : ?>
        <div class="day">
            <div class="title"><?php echo date( 'l jS F Y', strtotime( $day ) ); ?></div>
            <div class="posts">
            <?php foreach( $day_posts as $post ) : setup_postdata( $post ); ?>
                <div class="post">
                    <div class="title"><?php the_title(); ?></div>
                    <div class="content"><?php the_content(); ?></div>
                </div>          
            <?php endforeach; ?>
            </div>
        </div>  
    <?php endforeach; wp_reset_postdata(); ?>
    </div>
<?php endif; ?>

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

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