widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
// 先统计每个年份和月份的文章数量
$yearCount = array();
$monthCount = array();
$allPosts = array();
// 第一次遍历:收集所有文章并统计数量
while($archives->next()):
$year_tmp = date('Y', $archives->created);
$mon_tmp = date('m', $archives->created);
$yearMonth = $year_tmp . '-' . $mon_tmp;
// 统计年份文章数
if (!isset($yearCount[$year_tmp])) {
$yearCount[$year_tmp] = 0;
}
$yearCount[$year_tmp]++;
// 统计月份文章数
if (!isset($monthCount[$yearMonth])) {
$monthCount[$yearMonth] = 0;
}
$monthCount[$yearMonth]++;
// 处理标题长度
$title = $archives->title;
if (mb_strlen($title, 'UTF-8') > 30) {
$title = mb_substr($title, 0, 30, 'UTF-8') . '...';
}
// 存储文章信息
$allPosts[] = array(
'year' => $year_tmp,
'month' => $mon_tmp,
'created' => $archives->created,
'permalink' => $archives->permalink,
'title' => $title,
'original_title' => $archives->title, // 保留原始标题用于tooltip
'commentsNum' => $archives->commentsNum,
'categories' => $archives->categories
);
endwhile;
// 按年份和月份排序(从新到旧)
krsort($yearCount);
krsort($monthCount);
$output = '';
$currentYear = '';
$currentMonth = '';
foreach ($allPosts as $post):
$year_tmp = $post['year'];
$mon_tmp = $post['month'];
// 获取分类信息 - 如果有分类就显示,没有就不显示
$categoryDisplay = '';
if (!empty($post['categories'])) {
$category = current($post['categories']);
$categoryName = $category['name'];
$categoryPermalink = $category['permalink'];
$categoryDisplay = '['.'
'. $categoryName .''.']';
}
// 新年份开始
if ($currentYear != $year_tmp) {
// 关闭前一个月份和年份
if ($currentMonth != '') {
$output .= '
';
}
if ($currentYear != '') {
$output .= '