php 输出文章列表,WordPress按时间顺序循环输出文章列表
时间: 2024-01-04 12:03:29 浏览: 33
你可以使用 WordPress 提供的函数 `get_posts()` 来获取文章列表并按时间顺序排序,然后使用循环输出每篇文章。具体实现代码如下:
```php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, // 获取所有文章
'order' => 'DESC', // 按时间倒序排序
'orderby' => 'date'
);
$posts = get_posts($args);
foreach ($posts as $post) : setup_postdata($post);
// 输出文章标题和链接
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
endforeach;
wp_reset_postdata();
```
这段代码会获取所有文章并按时间倒序排序,然后使用 `foreach` 循环输出每篇文章的标题和链接。注意,需要在循环结束后调用 `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()`可以用来重置查询,以便在后续的代码中查询其他内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)