如何在最新的日期发布帖子?

时间:2013-01-14 作者:Meshi

有没有办法让所有帖子在最晚的日期发布?可能不一定是今天,可能是昨天或前天,甚至是上周的一天。

1 个回复
SO网友:Eugene Manuilov

您可以这样做:

if ( have_posts() ) :
    /* Queue the first post, that way we know
     * which date we\'re dealing with.
     *
     * We reset this later so we can run the loop
     * properly with a call to rewind_posts().
     */
    the_post();

    // receive the latest post timestamp
    $latest_post_timestamp = strtotime( get_post()->post_date );

    /* Since we called the_post() above, we need to
     * rewind the loop back to the beginning that way
     * we can run the loop properly, in full.
     */
    rewind_posts();

    // create our custom query and set our latest date arguments
    $the_query = new WP_Query( array(
        \'year\' => date( \'Y\', $latest_post_timestamp ),
        \'monthnum\' => date( \'m\', $latest_post_timestamp ),
        \'day\' => date( \'d\', $latest_post_timestamp ),
    ) );

    // render our latest date posts
    echo \'<ul>\';
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        echo \'<li>\' . get_the_title() . \'</li>\';
    endwhile;
    echo \'</ul>\';

    // Restore original query and post data
    wp_reset_query();
    wp_reset_postdata();
endif;

结束

相关推荐

“MANAGE_POSTS_CUSTOM_COLUMN”操作挂钩与“MANAGE_${POST_TYPE}_COLUMNS”筛选器挂钩有何关系?

怎样add_action(\"manage_posts_custom_column\", \"custom_callback_fun01\"); 与…有关add_filter(\"manage_{xxxx-xxx}_columns\", \"cusotm_callback_fun02\" );?它们是如何一起工作的?通过使用这两者,我们可以将自定义列添加到自定义帖子类型和显示日期,例如,在特色列中显示特色图像这是我的代码示例,它工作得很好,但我对过滤器如何与动作挂钩一起工作有点困惑?//slides&#x