PHP循环-调整时间轴归档页面
的归档页面非常好,但是对于用于创建它的PHP循环有一个问题 该网站的作者发布了以下代码: 有一部分我不清楚。如果时间间隔超过一年(如2007年的帖子,2008年和2009年没有帖子,2010年再次出现),则此代码将打印年度标题(带有空的PHP循环-调整时间轴归档页面,php,wordpress,Php,Wordpress,的归档页面非常好,但是对于用于创建它的PHP循环有一个问题 该网站的作者发布了以下代码: 有一部分我不清楚。如果时间间隔超过一年(如2007年的帖子,2008年和2009年没有帖子,2010年再次出现),则此代码将打印年度标题(带有空的) 我如何调整它以跳过这些空的年份?一般逻辑可以简单到(半伪代码): $posts=fetchFromDatabase('SELECT*FROM'posts'ORDER BY'posted'DESC'); //$posts=数组( //数组('posted'=>'
)
我如何调整它以跳过这些空的年份?一般逻辑可以简单到(半伪代码):
$posts=fetchFromDatabase('SELECT*FROM'posts'ORDER BY'posted'DESC');
//$posts=数组(
//数组('posted'=>'2010-09-13 12:42:31','title'=>…)
//数组(…)
// )
$currentYear=null;
foreach($posts作为$post){
$year=日期('Y',标准时间($post['posted']);
如果($year!=$currentYear){
printf('%s',$year);
$currentYear=$year;
}
echo$post['title'];
}
简言之:
- 从按日期排序的数据库中获取要显示的所有帖子
- 跟踪您输出的“当前年份”
- 进入新的一年后,输出它,更新当前的一年
这样,只输出现有职位的年份。替换了此代码块:
else if ( $prev_post_year != $post_year ) {
/* Close off the OL */
?>
</ol>
<?php
$working_year = $prev_post_year;
/* Print year headings until we reach the post year */
while ( $working_year > $post_year ) {
$working_year--;
?>
<h3 class="archive_year"><?php echo $working_year?></h3>
<?php
}
/* Open a new ordered list */
?>
<ol class="archives_list">
<?php
}
else if($prev\u post\u year!=$post\u year){
/*封锁封锁线*/
?>
这是Wordpress特有的一个普通PHP问题吗?@deceze只是一个普通PHP问题——少量WP内容可能会被忽略,但你应该将其标记为与Wordpress相关(就是这么做的),因此有类似问题的人可以很容易找到答案。
else if ( $prev_post_year != $post_year ) {
/* Close off the OL */
?>
</ol>
<?php
$working_year = $prev_post_year;
/* Print year headings until we reach the post year */
while ( $working_year > $post_year ) {
$working_year--;
?>
<h3 class="archive_year"><?php echo $working_year?></h3>
<?php
}
/* Open a new ordered list */
?>
<ol class="archives_list">
<?php
}
else if ( $prev_post_year != $post_year ) {
/* Close off the OL */
?>
</ol>
<h3 class="archive_year"><?php echo $post_year?></h3>
<ol class="archives_list">
<?php
}