主题移植小记:Typecho按年输出文章归档

Typecho按年输出归档页面这个功能,网上能检索到现成的代码,但对于移植主题复杂的html结构而言实现起来太过繁琐,以下是网上找到的代码,在此基础上进行修改太过烧脑,遂尝试使用更优雅的方式,html结构更清晰,方便主题移植时候根据原始html结构进行适配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
Typecho_Widget::widget('Widget_Contents_Post_Recent', 'pageSize='.
Typecho_Widget::widget('Widget_Stat')->publishedPostsNum)->to($archives);
$date_y=0;$date_m=0;$output = '';$huan=0;
while($archives->next()){
$date_t = explode("","", date('Y,m,d', $archives->created));
if($date_y > $date_t[0]){
$date_m = 0;
$article_nums[] = $article_num;
$output .= '</ul></li></ul>';
}
if($date_y != $date_t[0]){
$date_y = $date_t[0];$article_num=0;
$article_flag[] = $tmp_flag = 'archives_'.$huan++;
$output .= '<h2>'.$date_y.' <span>×'. $tmp_flag .'</span></h2><ul>';
}
$output .= '<li><time>'.$date_t[1].'.'.$date_t[2].'</time> <a href=""'.$archives->permalink.'"">'.$archives->title.'</a> <sup><a href=""'.$archives->permalink.'#comment"">'.$archives->commentsNum.'</a></sup></li>';
$article_num++;
}
$article_nums[] = $article_num;
$output .= '</ul></li></ul>';
echo str_replace($article_flag, $article_nums, $output);
?>

首先需要把以下代码放入functions.php中,在Cherry主题中测试没什么BUG。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 文章归档
class AnnualArchive extends Typecho_Widget
{
private $_groupedYears = [];

public function execute()
{
$posts = $this->widget('Widget_Contents_Post_Recent', [
'pageSize' => 9999,
'status' => 'publish'
]);

$this->_groupedYears = $this->processPosts($posts);
}

private function processPosts($posts)
{
$grouped = [];
$posts->to($posts);

while ($posts->next()) {
$post = $posts->row;
$created = $post['created'];
$timestamp = ($created instanceof Typecho_Date)
? $created->time
: (is_numeric($created) ? $created : 0);

$year = date('Y', $timestamp);
$monthDay = date('m-d', $timestamp);

if (!isset($grouped[$year])) {
$grouped[$year] = [
'count' => 0,
'posts' => [],
'year' => $year
];
}

$grouped[$year]['posts'][] = [
'title' => $posts->title,
'permalink' => $posts->permalink,
'date' => $monthDay
];
$grouped[$year]['count']++;
}

krsort($grouped);
return $grouped;
}

public function getArchiveData()
{
return $this->_groupedYears;
}
}

这一段代码放置到需要的地方,例如归档页面模板,html结构根据需求来修改就好了。

1
2
3
4
5
6
7
8
9
10
11
12
<?php $archive = $this->widget('AnnualArchive'); $years = $archive->getArchiveData();?>
<?php foreach ($years as $yearData): ?>
<div class=""mod-archive-name""><?php echo $yearData['year']; ?></div>
<ul class=""mod-archive-list"">
<?php foreach ($yearData['posts'] as $post): ?>
<li>
<time class=""mod-archive-time text-nowrap me-4""><?php echo $post['date']; ?></time>
<a href=""<?php echo $post['permalink']; ?>"" title=""<?php echo $post['title']; ?>""><?php echo $post['title']; ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

给博客添加一个自动更新基金持仓盈亏的页面

本文介绍如何为博客添加自动更新基金持仓盈亏的页面。通过使用天天基金的API接口,实现分散持仓的统一查看。方案包含三个文件: 1. **update_funds.php**:PHP脚本,用于定期从天天基金API获取最新净值并更新本地数据文件。 2. **funds.json**:JSON格式的数据文件,存储基金名称、代码、成本价、份额等手动配置信息,脚本会自动更新最新净值和更新时间。 3. **前端模板文件**:用于展示持仓信息,读取funds.json计算收益和收益率。 使用方法:将脚本部署到支持PHP的服务器,配置funds.json中的持仓信息,可设置计划任务定期执行更新。新增`update_enabled`字段可控制是否跳过某只基金的更新(如清仓后)。前端通过读取JSON文件动态展示持仓盈亏情况。
阅读更多

Hello World

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.