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(在新視窗中開啟)
  • 按一下即可分享至 Skype(在新視窗中開啟)
  • 按一下即可以電子郵件傳送連結給朋友(在新視窗中開啟)
  • 點這裡列印(在新視窗中開啟)

相關


教學資源

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
限時免費
ESET NOD32
WINDOWS 10 &11 INSIDER PREVIEW
Windows 軟件下載
系統軟件
辦公軟件
圖像處理
影音媒體
網絡軟件
應用軟件
Mac 軟件下載
安卓軟件下載
網絡資訊
Mac資訊
Linux資訊
VPS資訊
NASA資訊
金融資訊
WhatsApp Stickers教學
WordPress資訊
WeChat資訊
PHP資訊
Plesk資訊
TensorFlow
教學資源
開源程序
網頁工具
SEO工具
醫療健康
旅遊及消閒
其他資訊
Content from
Content to
2018 年 10 月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 9 月   11 月 »

分類

  • 網站公告
  • 限時免費
  • ESET NOD32
  • WINDOWS 10 &11 INSIDER PREVIEW
  • Windows 軟件下載
  • 系統軟件
  • 辦公軟件
  • 圖像處理
  • 影音媒體
  • 網絡軟件
  • 應用軟件
  • Mac 軟件下載
  • 安卓軟件下載
  • 網絡資訊
  • Mac資訊
  • Linux資訊
  • VPS資訊
  • NASA資訊
  • WhatsApp Stickers教學
  • WordPress資訊
  • WeChat資訊
  • PHP資訊
  • Plesk資訊
  • TensorFlow
  • 教學資源
  • 開源程序
  • 網頁工具
  • SEO工具
  • 醫療健康
  • 旅遊及消閒
  • 其他資訊

彙整

近期文章

  • Windows 11 22H2更新為所有PC帶來全屏小工具 2023-01-29
  • 網絡神經科學理論- 實現智力的最佳預測 2023-01-29
  • 水族館鯊魚在有雄性在場的情況下出現”孤雌生殖”的奇特案例 2023-01-29
  • 《滿江紅》連發數條微博回應爭議:莫須有欲加之罪何患無辭 2023-01-29
  • 麻省理工學院開發出超低噪音無人機螺旋槳 2023-01-29
  • NASA為深空任務提供的革命性的推進設計 2023-01-29
  • 天文學家可能已經發現星系如何改變它們的形狀 2023-01-29
  • 健康的植物性飲食可以降低男性患結腸癌的風險 2023-01-29
  • 斯坦福大學推出DetectGPT 幫助教育工作者反擊ChatGPT生成的論文 2023-01-29
  • 小行星採礦在未來可能成為一件非常現實的事情 2023-01-29

熱門文章與頁面︰

  • DP vs HDMI 誰才是遊戲玩家最佳選擇?
  • ESET NOD32 LICENSE KEY (UPDATED 2023-01-17)
  • Explorer Patcher:讓Windows 11恢復Windows 10的行為特徵
  • Autodesk AutoCAD 2021 正式版註冊版-簡體/繁體中文/英文版
  • ESET NOD32 LICENSE KEY (UPDATED 2022-01-01)
  • 微軟強化Game Bar:可顯示Xbox手柄剩餘電量
  • 打車叫到特斯拉不會開門很尷尬?官方介紹開關門方法
  • 配備F1發動機:梅賽德斯-AMG One量產車終於要來了
  • CCleaner全家桶激活密鑰
  • 自來水、白開水、純淨水?有些水絕不能用到加濕器

投遞稿件

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