Skip to content
WONGCW 網誌
  • 首頁
  • 論壇
  • 微博
  • 壁紙下載
  • 免費圖床
  • 視頻下載
  • 聊天室
  • SEO工具
  • 支援中心
  • 表格製作
  • More
    • 在線名片
    • 網頁搜索
    • 天氣預報
    • 二維碼生成器
  • Search Icon

WONGCW 網誌

記錄生活經驗與點滴

typecho单页与文章页自定义字段方法[二次开发]

typecho单页与文章页自定义字段方法[二次开发]

2018-10-12 Comments 0 Comment

自定义字段这个东西虽然typecho里面自己带了一个themeFields方法,但是新人用起来终究会有些尴尬,而且网上也没啥教程,今天来教大家一个比较简单的方式,就是无论你是否有基础,都会操作的一个方法。


简述

首先理清思路,我们需要对post和page两个类型的页面来添加不同的自定义字段,说到自定义字段,我们可能会想到数据库啥的东西,没错,我们首先要去post和page对应的表中来进行添加字段,然后实现字段写入,最终达到可调用的效果,这就是今天文章的主体思路,接着往下看;

post 页面新增字段方法

post 页面其实就是文章发布页面

首先这个方法我是从我的好基佬seogo那儿看到的,先无情的抄袭一波好了,给整合整合。

我现在需要在文章里面体现一个头图,市场价,优惠价,然后填写数值直接调用。

  • 数据库中新增字段,使用Navicat for MySQL数据库管理工具(不要问我为啥要使用这个工具)如下
  • 在表typecho_contents中新增的3个字段的名称分别为conImg 文章头图,comPrice 优惠价,comOprice 原价

羊毛党之家 typecho单页与文章页自定义字段方法[二次开发]  https://yangmaodang.org

 

  • 在后台文章发布页面中添加可供填写的表单
    • 涉及文件修改 :\admin\write-post.php
  • 进入到对应文件的编辑界面,在你认为合适的地方添加如下代码(我是网址缩略名下面儿加的)
<p>
    <input type="text" name="conImg" class="w-100" placeholder="<?php _e('头图链接'); ?>" value="<?php $post->conImg(); ?>"/>
    <input type="text" name="comOprice" placeholder="<?php _e('市场价'); ?>" value="<?php $post->comOprice(); ?>"/>
    <input type="text" name="comPrice" placeholder="<?php _e('现价'); ?>" value="<?php $post->comPrice(); ?>"/> 
</p>
// name,value,内的字段一定要匹配才行,而且还得和数据库中加入的字段名匹配,不要错,细心一点儿
  • 文章发布页面展示效果如下

 

  • 给writePost函数添加写入字段,文件路径:\var\Widget\Contents\Post\Edit.php,代码如下
// 查询 public function writePost() 参照如下添加
public function writePost()
    {
        $contents = $this->request->from('password', 'allowComment',
            'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility', 'conImg', 'comPrice', 'comOprice');
  • 给insert函数添加构建插入结构,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function insert(array $content) 参照如下添加
public function insert(array $content)
    {
        /** 构建插入结构 */
        $insertStruct = array(
            'title'         =>  empty($content['title']) ? NULL : htmlspecialchars($content['title']),
            'created'       =>  empty($content['created']) ? $this->options->time : $content['created'],
            'modified'      =>  $this->options->time,
            'text'          =>  empty($content['text']) ? NULL : $content['text'],
            'order'         =>  empty($content['order']) ? 0 : intval($content['order']),
            'authorId'      =>  isset($content['authorId']) ? $content['authorId'] : $this->user->uid,
            'template'      =>  empty($content['template']) ? NULL : $content['template'],
            'type'          =>  empty($content['type']) ? 'post' : $content['type'],
            'status'        =>  empty($content['status']) ? 'publish' : $content['status'],
            'password'      =>  empty($content['password']) ? NULL : $content['password'],
            'commentsNum'   =>  empty($content['commentsNum']) ? 0 : $content['commentsNum'],
            'allowComment'  =>  !empty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
            'allowPing'     =>  !empty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
            'allowFeed'     =>  !empty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
            'parent'        =>  empty($content['parent']) ? 0 : intval($content['parent']),
            // 如下三个是我新增加的三个字段及对应的参数
            'conImg'        =>  empty($content['conImg']) ? NULL : $content['conImg'],
            'comPrice'      =>  empty($content['comPrice']) ? NULL : $content['comPrice'],
            'comOprice'     =>  empty($content['comOprice']) ? NULL : $content['comOprice']
     );
  • 给update函数添加构建更新结构,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function update(array $content, Typecho_Db_Query $condition) 参照如下添加
public function update(array $content, Typecho_Db_Query $condition)
    {
        /** 首先验证写入权限 */
        if (!$this->isWriteable(clone $condition)) {
            return false;
        }

        /** 构建更新结构 */
        $preUpdateStruct = array(
            'title'         =>  empty($content['title']) ? NULL : htmlspecialchars($content['title']),
            'order'         =>  empty($content['order']) ? 0 : intval($content['order']),
            'text'          =>  empty($content['text']) ? NULL : $content['text'],
            'template'      =>  empty($content['template']) ? NULL : $content['template'],
            'type'          =>  empty($content['type']) ? 'post' : $content['type'],
            'status'        =>  empty($content['status']) ? 'publish' : $content['status'],
            'password'      =>  empty($content['password']) ? NULL : $content['password'],
            'allowComment'  =>  !empty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
            'allowPing'     =>  !empty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
            'allowFeed'     =>  !empty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
            'parent'        =>  empty($content['parent']) ? 0 : intval($content['parent']),
            // 如下三个是我新增加的三个字段及对应的参数
            'conImg'        =>  empty($content['conImg']) ? NULL : $content['conImg'],
            'comPrice'      =>  empty($content['comPrice']) ? NULL : $content['comPrice'],
            'comOprice'     =>  empty($content['comOprice']) ? NULL : $content['comOprice']
     );
  • 给select函数里添加查询新字段,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function select() 参照如下添加
// 新增加了 table.contents.conImg , table.contents.comPrice , table.contents.comOprice
public function select()
{
    return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
    'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
    'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
    'table.contents.parent', 'table.contents.conImg', 'table.contents.comPrice', 'table.contents.comOprice')->from('table.contents');
}

post 页面新增字段调用方法

// 调用原价
<?php $this->comOprice() ?>
// 调用现价
<?php $this->comPrice() ?>
// 调用缩略图
<?php $this->conImg() ?>

page 页面新增字段方法

page 页面其实就是独立页面

确定思路,做了一个样式,独立页面调用出来了之后,我还想在页面名称上边或者左右显示每个独立页面的自己的图标

有些方法和post页面添加字段一样,具体的请接着往下看

  • 数据库中表typecho_contents增加我自己事先想好的字段fontawesome,如下图

羊毛党之家 typecho单页与文章页自定义字段方法[二次开发]  https://yangmaodang.org

  • 在后台文章发布页面中添加可供填写的表单
    • 涉及文件修改 :\admin\write-page.php,记住,这里是write-page
  • 进入到对应文件的编辑界面,在你认为合适的地方添加如下代码(我是在侧栏发布时间的下面儿加的,我觉的美观)
// 千万注意一点value 里面的页面参数应该是 $page->fontawesome(); 
// 而且还得注意,name,value里面的字段名称一定要和数据库中新增加的匹配上,细心点儿
<section class="typecho-post-option">
    <label for="fontawesome" class="typecho-label"><?php _e('Font Awesome图标'); ?></label>
    <p><input type="text" name="fontawesome" placeholder="<?php _e('fontawesome'); ?>" value="<?php $page->fontawesome(); ?>"/> </p>
    <p class="description"><?php _e('自豪的采用了<a href="http://fontawesome.dashgame.com/" target="_blank">Font Awesome</a>字体图标'); ?></p>
</section>
  • 独立页面展示效果如下

 

  • 给writePage函数添加写入字段,文件路径:\var\Widget\Contents\Page\Edit.php,代码如下
// 查询 public function writePage() 参照如下添加
public function writePage()
    {
        $contents = $this->request->from('text', 'template', 'allowComment',
            'allowPing', 'allowFeed', 'slug', 'order', 'visibility', 'fontawesome');
  • 给insert函数添加构建插入结构,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function insert(array $content) 参照如下添加
public function insert(array $content)
    {
        /** 构建插入结构 */
        $insertStruct = array(
            'title'         =>  empty($content['title']) ? NULL : htmlspecialchars($content['title']),
            'created'       =>  empty($content['created']) ? $this->options->time : $content['created'],
            'modified'      =>  $this->options->time,
            'text'          =>  empty($content['text']) ? NULL : $content['text'],
            'order'         =>  empty($content['order']) ? 0 : intval($content['order']),
            'authorId'      =>  isset($content['authorId']) ? $content['authorId'] : $this->user->uid,
            'template'      =>  empty($content['template']) ? NULL : $content['template'],
            'type'          =>  empty($content['type']) ? 'post' : $content['type'],
            'status'        =>  empty($content['status']) ? 'publish' : $content['status'],
            'password'      =>  empty($content['password']) ? NULL : $content['password'],
            'commentsNum'   =>  empty($content['commentsNum']) ? 0 : $content['commentsNum'],
            'allowComment'  =>  !empty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
            'allowPing'     =>  !empty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
            'allowFeed'     =>  !empty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
            'parent'        =>  empty($content['parent']) ? 0 : intval($content['parent']),
            // 如下三个是我新增加的三个字段及对应的参数
            'conImg'        =>  empty($content['conImg']) ? NULL : $content['conImg'],
            'comPrice'      =>  empty($content['comPrice']) ? NULL : $content['comPrice'],
            'comOprice'     =>  empty($content['comOprice']) ? NULL : $content['comOprice'],
            // 独立页面构建插入结构
            'fontawesome'   =>  empty($content['fontawesome']) ? NULL : $content['fontawesome']
            
     );
  • 给update函数添加构建更新结构,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function update(array $content, Typecho_Db_Query $condition) 参照如下添加
public function update(array $content, Typecho_Db_Query $condition)
    {
        /** 首先验证写入权限 */
        if (!$this->isWriteable(clone $condition)) {
            return false;
        }

        /** 构建更新结构 */
        $preUpdateStruct = array(
            'title'         =>  empty($content['title']) ? NULL : htmlspecialchars($content['title']),
            'order'         =>  empty($content['order']) ? 0 : intval($content['order']),
            'text'          =>  empty($content['text']) ? NULL : $content['text'],
            'template'      =>  empty($content['template']) ? NULL : $content['template'],
            'type'          =>  empty($content['type']) ? 'post' : $content['type'],
            'status'        =>  empty($content['status']) ? 'publish' : $content['status'],
            'password'      =>  empty($content['password']) ? NULL : $content['password'],
            'allowComment'  =>  !empty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
            'allowPing'     =>  !empty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
            'allowFeed'     =>  !empty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
            'parent'        =>  empty($content['parent']) ? 0 : intval($content['parent']),
            // 如下三个是我新增加的三个字段及对应的参数
            'conImg'        =>  empty($content['conImg']) ? NULL : $content['conImg'],
            'comPrice'      =>  empty($content['comPrice']) ? NULL : $content['comPrice'],
            'comOprice'     =>  empty($content['comOprice']) ? NULL : $content['comOprice'],
            // 独立页面构建更新结构
            'fontawesome'   =>  empty($content['fontawesome']) ? NULL : $content['fontawesome']
     );
  • 给select函数里添加查询新字段,文件路径:\var\Widget\Abstract\Contents.php
// 查询 public function select() 参照如下添加
// 新增加了一个 table.contents.fontawesome
public function select()
{
    return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
    'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
    'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
    'table.contents.parent', 'table.contents.conImg', 'table.contents.comPrice', 'table.contents.comOprice', 'table.contents.fontawesome')->from('table.contents');
}

page 页面新增字段调用方法

// 独立页面图标
<?php $pages->fontawesome() ?>

分享此文:

  • 按一下即可分享至 X(在新視窗中開啟) X
  • 按一下以分享至 Facebook(在新視窗中開啟) Facebook
  • 分享到 WhatsApp(在新視窗中開啟) WhatsApp
  • 按一下以分享到 Telegram(在新視窗中開啟) Telegram
  • 分享到 Pinterest(在新視窗中開啟) Pinterest
  • 分享到 Reddit(在新視窗中開啟) Reddit
  • 按一下即可以電子郵件傳送連結給朋友(在新視窗中開啟) 電子郵件
  • 點這裡列印(在新視窗中開啟) 列印

相關


教學資源

Post navigation

PREVIOUS
Debian 9启用Google BBR的方法,实现TCP加速
NEXT
Android偷拍神器APP@Background Video Recorder@你已上鏡了~

發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料。

More results...

Generic filters
Exact matches only
Search in title
Search in content
Search in excerpt
Filter by 分類
網站公告
Featured
限時免費
Windows 軟件下載
系統軟件
辦公軟件
圖像處理
影音媒體
網絡軟件
應用軟件
Mac 軟件下載
安卓軟件下載
網絡資訊
Mac資訊
Linux資訊
VPS資訊
NASA資訊
WordPress資訊
WeChat資訊
PHP資訊
教學資源
開源程序
網頁工具
SEO工具
醫療健康
其他資訊
Content from
Content to
2018 年 10 月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 9 月   11 月 »

分類

  • 網站公告
  • 限時免費
  • Windows 軟件下載
  • 系統軟件
  • 辦公軟件
  • 圖像處理
  • 影音媒體
  • 網絡軟件
  • 應用軟件
  • Mac 軟件下載
  • 安卓軟件下載
  • 網絡資訊
  • Mac資訊
  • Linux資訊
  • VPS資訊
  • NASA資訊
  • WordPress資訊
  • WeChat資訊
  • PHP資訊
  • 教學資源
  • 開源程序
  • 網頁工具
  • SEO工具
  • 醫療健康
  • 其他資訊

彙整

近期文章

  • 消息稱Manus母公司擬融資1億美元資金用於發展中國業務 2025-05-14
  • Unity威脅撤銷《DayZ》開發者工作室的軟體授權 2025-05-14
  • 特斯拉煥新Model Y遇冷:上市即促銷沒有”新”賣點 2025-05-14
  • 從太空俯瞰的生態警報:高山植物基因庫正加速流失 2025-05-14
  • 宇宙壽命大縮水科學家將毀滅倒數提前萬億倍 2025-05-14
  • 疫苗難產、藥物稀缺人類與真菌的戰鬥正陷入困境 2025-05-14
  • AI顛覆製藥業:數月設計抗體藥物挑戰傳統研發 2025-05-14
  • 大疆確認Mavic 4 Pro將不會在美國上市 2025-05-14
  • 京東CEO:騎手工服嚴重缺貨正在趕工 2025-05-14
  • 白宮大幅削減小額包裹關稅稅率美國業界卻有另一番見解 2025-05-14

熱門文章與頁面︰

  • 您可以在Windows 11 24H2 中找回WordPad
  • 巴西總統盧拉見證美團簽署10億美元投資協議Keeta宣布進入巴西市場
  • 傳三星2nm良品率逐漸提高英偉達及高通都有下單意向
  • 台積電美國三座新廠產能預訂一空蘋果、NVIDIA、AMD搶著要
  • 2024全球晶片公司排行:NVIDIA居首英飛凌、義法半導體跌出前十
  • 蘋果今年將為台積電貢獻1兆新台幣營收
  • Manus回應開放註冊:海外用戶已取消等候名單國內產品尚未發布
  • 海爾Leader三筒懶人洗衣機發表國補後3,999元起
  • 傳NVIDIA全球總部將設在台灣黃仁勳將於下周宣布選址
  • 特斯拉AI代理商上線:幫助客戶更輕鬆接觸公司高層

投遞稿件

歡迎各界人士投遞稿件到admin@wongcw.com

請提供以下資料:

1.你的名字

2.你的電郵

3.分類目錄

4.文章標題

5.文章摘要

6.文章內容

7.文章來源

 

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

快帆

MALUS

極度掃描

DMCA.com Protection Status

WONGCW 網誌

  • 免責聲明
  • 捐助我們
  • ThemeNcode PDF Viewer
  • ThemeNcode PDF Viewer SC
  • Events

服務器提供

本站使用之服務器由ikoula提供。

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

© 2025   All Rights Reserved.