php 输出文章列表,WordPress按时间顺序循环输出文章列表
时间: 2023-12-03 08:45:18 浏览: 36
在 WordPress 中,可以使用 `WP_Query` 类来查询文章列表,并且按照时间顺序进行排序。以下是一个示例代码:
```
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, // 显示所有文章
'orderby' => 'date', // 按照时间排序
'order' => 'DESC' // 倒序排列
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// 输出文章信息
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . get_the_excerpt() . '</p>';
}
wp_reset_postdata();
} else {
// 如果没有文章,则输出提示信息
echo '没有文章!';
}
```
注意,在循环输出文章后,需要使用 `wp_reset_postdata()` 函数来重置文章查询,以免影响后面的文章查询。
相关问题
WordPress 循环输出今天 昨天 7天后的阅读文章列表
可以使用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()`可以用来重置查询,以便在后续的代码中查询其他内容。
WordPress 循环输出 今天的文章 昨天的文章 7天内的文章 的阅读文章列表
可以使用WordPress的默认函数`WP_Query()`来获取指定时间范围内的文章列表。以下是一个示例代码:
```
<?php
$today = getdate(); // 获取当前日期
$args_today = array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
),
'orderby' => 'date',
'order' => 'DESC',
);
$args_yesterday = array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'] - 1,
),
),
'orderby' => 'date',
'order' => 'DESC',
);
$args_7days = array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => '-7 days', // 查询7天前到现在的文章
'before' => $today['year'] . '-' . $today['mon'] . '-' . $today['mday'], // 查询今天之前的文章
'inclusive' => true,
),
),
'orderby' => 'date',
'order' => 'DESC',
);
// 查询今天的文章
$the_query = new WP_Query( $args_today );
if ( $the_query->have_posts() ) :
echo '<h3>今天的文章</h3>';
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<div>' . get_the_excerpt() . '</div>';
endwhile;
endif;
wp_reset_postdata();
// 查询昨天的文章
$the_query = new WP_Query( $args_yesterday );
if ( $the_query->have_posts() ) :
echo '<h3>昨天的文章</h3>';
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<div>' . get_the_excerpt() . '</div>';
endwhile;
endif;
wp_reset_postdata();
// 查询7天内的文章
$the_query = new WP_Query( $args_7days );
if ( $the_query->have_posts() ) :
echo '<h3>7天内的文章</h3>';
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<div>' . get_the_excerpt() . '</div>';
endwhile;
endif;
wp_reset_postdata();
?>
```
这段代码会分别查询今天的文章、昨天的文章和7天内的文章,并按照时间倒序输出所有文章的标题和摘要。如果需要输出更多信息,可以在循环中添加对应的WordPress函数。`wp_reset_postdata()`可以用来重置查询,以便在后续的代码中查询其他内容。