获取模板自定义字段值

前言

在 Typecho 很多模板都要通过设置自定义字段来实现文章缩略图或者其他功能,但是我们在二次开发或者开发插件时,并没有一个接口来实现获取自定义字段,所以便有了我今天的想法。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public function getCustom($cid, $key){
$db = Typecho_Db::get();
$rows = $db->fetchAll($db->select('table.fields.str_value')->from('table.fields')
->where('table.fields.cid = ?', $cid)
->where('table.fields.name = ?', $key)
);
// 如果有多个值则存入数组
foreach ($rows as $row) {
$img = $row['str_value'];
if (!empty($img)) {
$values[] = $img;
}
}
return $values;
}

使用

使用时只要使用$this->getCustom(mix $cid, mix $key)就可以了,两个参数分别是文章cid自定义字段名,函数会把自定义内容返回成数组。

来源:https://www.moleft.cn/post-178.html


获取模板自定义字段值
http://yoursite.com/2022/08/17/typecho/getCustom/
作者
雨停之後
发布于
2022年8月17日
许可协议