wp博客如何实现文章归档页面 具体 代码
要实现文章归档页面,可以按照以下步骤进行操作:
-
创建一个新页面,命名为“归档”或“文章归档”。
-
在该页面中添加以下代码:
<div class="archives">
<h2 class="archive-title">文章归档</h2>
<ul>
<?php
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
foreach($myposts as $post) :
setup_postdata($post);
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
if($year != $previous_year || $month != $previous_month) :
if($ul_open == true) :
echo '</ul>';
endif;
echo '<h3 class="archive-year">' . $year . ' 年 ' . $month . ' 月</h3>';
echo '<ul class="archive-month-list">';
$ul_open = true;
endif;
$previous_year = $year; $previous_month = $month;
?>
<li>
<span class="archive-date"><?php echo $day; ?> 日</span>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
-
保存并发布该页面。
-
在WordPress后台中,进入外观 -> 菜单,将“归档”页面添加到菜单中。
-
刷新网站页面,点击菜单中的“归档”,即可查看文章归档页面。
注意:以上代码需要放在WordPress主题的页面模板文件中,如果不知道如何编辑主题文件,请先备份好文件再进行操作
原文地址: https://www.cveoy.top/t/topic/cSzh 著作权归作者所有。请勿转载和采集!