Skip to content
WONGCW 網誌
  • 首頁
  • 論壇
  • 微博
  • 壁紙下載
  • 免費圖床
  • 視頻下載
  • 聊天室
  • SEO工具
  • 支援中心
  • 表格製作
  • More
    • 在線名片
    • 網頁搜索
    • 天氣預報
    • 二維碼生成器
    • WordPress 插件及主題下載
  • 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() ?>

分享此文:

  • 分享到 Twitter(在新視窗中開啟)
  • 按一下以分享至 Facebook(在新視窗中開啟)
  • 分享到 WhatsApp(在新視窗中開啟)
  • 按一下以分享到 Telegram(在新視窗中開啟)
  • 分享到 Pinterest(在新視窗中開啟)
  • 分享到 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工具
  • 醫療健康
  • 其他資訊

彙整

近期文章

  • 出貨量大漲60% 中國智慧型手機市場正在復甦 2023-12-06
  • 2024年央視春晚吉祥物正式亮相:五爪金龍造型命名“龍辰辰” 2023-12-06
  • 全球動力電池裝車量公佈寧德時代獨佔1/3 2023-12-06
  • 《絕地求生》史上最大地圖今日上線:史無前例兩架飛機風景美輪美奐 2023-12-06
  • 美國心臟協會稱黴黴新歌可救命:節奏和心肺復甦術相同 2023-12-06
  • OpenAI CEO Altman:董事會正在重新思考公司架構 2023-12-06
  • 特斯拉已在歐洲多個國家興建V4超級充電樁充電站包括德國西班牙 2023-12-06
  • 龍芯中科遭中科百孚累計減持超5% 2023-12-06
  • 串流平台Netflix將於年底前下架大量影視作品 2023-12-06
  • 紐西蘭航空訂購亞馬遜支援的電動飛機擬於2026年投入使用 2023-12-06

熱門文章與頁面︰

  • 打車叫到特斯拉不會開門很尷尬?官方介紹開關門方法
  • 鳥寶寶有多內捲?為了讓父母投餵自己長出恐怖的嘴巴
  • 全球首型、全球最大24000TEU級核動力貨櫃船公佈:採用第四代堆型熔鹽反應器
  • 比特幣市值已超越伯克希爾和Meta
  • 黃仁勳:華為是英偉達在AI晶片領域的強大競爭對手
  • SpaceX星際飛船第三次試飛將在聖誕節前:示範“空中加油”
  • 4款家用血糖儀、尿酸儀對比評測
  • 思科推出人工智慧助理增強安全雲端平台的網路安全性
  • 盜版Windows 7還能免費升級Windows 10嗎?
  • WatchTube可讓用戶在Apple Watch上直接觀看YouTube視頻

投遞稿件

歡迎各界人士投遞稿件到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

© 2023   All Rights Reserved.