WordPress 循环输出今天 昨天 7天后的阅读文章列表
时间: 2024-02-15 18:02:05 浏览: 26
可以使用WordPress的默认函数`WP_Query()`来获取指定时间范围内的文章列表。以下是一个示例代码:
```
<?php
$today = getdate(); // 获取当前日期
$args = array(
'post_type' => 'post',
'date_query' => array(
'after' => '-7 days', // 查询7天前到现在的文章
'before' => $today['year'] . '-' . $today['mon'] . '-' . $today['mday'], // 查询今天之前的文章
'inclusive' => true,
),
'orderby' => 'date',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div><?php the_excerpt(); ?></div>
<?php endwhile; endif; wp_reset_postdata(); ?>
```
这段代码会查询7天前到现在的文章,并按照时间倒序输出所有文章的标题和摘要。如果需要输出更多信息,可以在循环中添加对应的WordPress函数。`wp_reset_postdata()`可以用来重置查询,以便在后续的代码中查询其他内容。