马春杰杰 Exit Reader Mode

[开源项目] Typecho插件-自定义字段-CustomFields

一个Typecho插件,用于在后台自定义文章的字段,目前开源在GitHubGitee

GitHubma3252788/CustomFields: Typecho插件-自定义字段-CustomFields (github.com)

Gitee公开分享/Typecho插件-自定义字段-CustomFields (gitee.com)

插件介绍

插件使用很简单,下载之后将文件夹命名为CustomFields,在后台启用插件。

进入配置页面,按照json格式进行配置,例如:

[
    {
        "name": "Bettor",
        "label": "打赌人",
        "description": "参与打赌的人"
    },
    {
        "name": "Gamble",
        "label": "赌约",
        "description": "打赌的内容是什么"
    },
    {
        "name": "MaStake",
        "label": "马春杰赌注",
        "description": "马春杰输了怎么办?"
    },
    {
        "name": "DuStake",
        "label": "杜丽娜赌注",
        "description": "杜丽娜输了怎么办?"
    },
    {
        "name": "WhoWin",
        "label": "谁赢了",
        "description": "赢者何人?"
    },
    {
        "name": "emails",
        "label": "邮件通知",
        "description": "通知给谁?"
    }
]

在新建文章的时候就可以在下方看到自定义的字段了:

自定义字段引用

在插件中通过文章cid引用字段:

$article_cid = $comment->cid;
function getCustom($cid, $key){
            $f=Typecho_Widget::widget('Widget_Archive@'.$cid,'pageSize=1&type=post', 'cid='.$cid);
            return $f->fields->$key;
            }
$field = getCustom($article_cid, 'Bettor');

在文章页面通过this引用:

$field = $this->fields->Bettor;

判断字段是否存在:

if(isset($this->fields->Bettor)){
  echo '字段存在,值为:'.$this->fields->Bettor;
}else{
  echo '字段不存在';
}

通过post引用:

<?php echo $post['fields']['Bettor'];?>