Dei*_*mos 1 php wordpress wordpress-theming
我正在尝试按日期和时间获取 WordPress 帖子,例如从 02/01/2014 到 02/05/2014 和时间 17:10,有人知道吗?
谢谢您的帮助
WP_Query 类可以实现这一点。只需设置一个新查询,传入适当的参数:
$args = array(
'date_query' => array(
// limit to between these dates
array(
'after' => '2014-02-01',
'before' => '2014-02-05', //remove this line if no upper limit
'inclusive' => true,
),
// limit to posts before 17:10 (not tested)
array(
'hour' => 17,
'minute' => 10,
'compare' => '<=',
),
// limit to posts after 08:30
array(
'hour' => 08,
'minute' => 30,
'compare' => '>=',
),
),
'posts_per_page' => -1,
);
$my_date_query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)
然后使用您刚刚实例化的查询对象运行循环:
<?php if ( $my_date_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $my_date_query->have_posts() ) : $my_date_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
日期参数部分很好地概述了您可以创建的帖子查询类型:http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
注意不要使用query_posts. 来自法典:
query_posts() 是一种过于简单且有问题的方法,通过将其替换为新的查询实例来修改页面的主查询。它效率低下(重新运行 SQL 查询)并且在某些情况下会彻底失败
| 归档时间: |
|
| 查看次数: |
2317 次 |
| 最近记录: |