f
Typecho文章页添加自定义字段的方法
Typecho文章页添加自定义字段的方法
二呆 5年前 (2018-12-09) typecho开发  #Typecho# 
浏览:7353
wordpress升级到5.0写作起来还真有点不适应界面,不过看起来很新颖,那就用5.0记录一下Typecho文章页添加自定义字段的方法体验一下,结果5.0太不适应了,就还原了下旧版本。 如果想还原旧版本,可以手动下载旧版本的wordpress程序删掉wp-content/和wp-config.php覆盖到网站目录即可,或者安装WP Downgrade插件自动完成还原旧版本工作。

方法一:使用新建表字段方式添加 1、首先在contents表中新建一个test字段,类型可为字符串。 2、后台模板文件write-post.php 表单中插入如下代码:

  1. <p><input type="text" name="test" value="<?php $post->test_url(); ?>"/></p>

3、在 Widget\Contents\Post\Edit.php 这里的 writePost 函数里需要接收新字段参数:

  1. public function writePost(){
  2.         $contents = $this->request->from('password', 'allowComment',
  3.             'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility','test_url');

4-1、Widget\Abstract\Contents.php 这里的 insert 函数添加新参数:

  1. /**
  2.      * 插入内容
  3.      * @access public
  4.      * @param array $content 内容数组
  5.      * @return integer
  6.      */
  7.     public function insert(array $content){
  8.         /** 构建插入结构 */
  9.         $insertStruct = array(
  10.             'title'         =>  emptyempty($content['title']) ? NULL : htmlspecialchars($content['title']),
  11.             'created'       =>  emptyempty($content['created']) ? $this->options->gmtTime : $content['created'],
  12.             'modified'      =>  $this->options->gmtTime,
  13.             'text'          =>  emptyempty($content['text']) ? NULL : $content['text'],
  14.             'order'         =>  emptyempty($content['order']) ? 0 : intval($content['order']),
  15.             'authorId'      =>  isset($content['authorId']) ? $content['authorId'] : $this->user->uid,
  16.             'template'      =>  emptyempty($content['template']) ? NULL : $content['template'],
  17.             'type'          =>  emptyempty($content['type']) ? 'post' : $content['type'],
  18.             'status'        =>  emptyempty($content['status']) ? 'publish' : $content['status'],
  19.             'password'      =>  emptyempty($content['password']) ? NULL : $content['password'],
  20.             'commentsNum'   =>  emptyempty($content['commentsNum']) ? 0 : $content['commentsNum'],
  21.             'allowComment'  =>  !emptyempty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
  22.             'allowPing'     =>  !emptyempty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
  23.             'allowFeed'     =>  !emptyempty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
  24.             'parent'        =>  emptyempty($content['parent']) ? 0 : intval($content['parent']),
  25.             'test_url'         =>  emptyempty($content['test_url']) ? NULL : $content['test_url']
  26.         );

4-2、这里的update函数里构建更新结构加入新字段:

  1. /**
  2.      * 更新内容
  3.      * @access public
  4.      * @param array $content 内容数组
  5.      * @param Typecho_Db_Query $condition 更新条件
  6.      * @return integer
  7.      */
  8.     public function update(array $content, Typecho_Db_Query $condition)
  9.     {
  10.         /** 首先验证写入权限 */
  11.         if (!$this->isWriteable(clone $condition)) {
  12.             return false;
  13.         }
  14.         /** 构建更新结构 */
  15.         $preUpdateStruct = array(
  16.             'title'         =>  emptyempty($content['title']) ? NULL : htmlspecialchars($content['title']),
  17.             'order'         =>  emptyempty($content['order']) ? 0 : intval($content['order']),
  18.             'text'          =>  emptyempty($content['text']) ? NULL : $content['text'],
  19.             'template'      =>  emptyempty($content['template']) ? NULL : $content['template'],
  20.             'type'          =>  emptyempty($content['type']) ? 'post' : $content['type'],
  21.             'status'        =>  emptyempty($content['status']) ? 'publish' : $content['status'],
  22.             'password'      =>  emptyempty($content['password']) ? NULL : $content['password'],
  23.             'allowComment'  =>  !emptyempty($content['allowComment']) && 1 == $content['allowComment'] ? 1 : 0,
  24.             'allowPing'     =>  !emptyempty($content['allowPing']) && 1 == $content['allowPing'] ? 1 : 0,
  25.             'allowFeed'     =>  !emptyempty($content['allowFeed']) && 1 == $content['allowFeed'] ? 1 : 0,
  26.             'parent'        =>  emptyempty($content['parent']) ? 0 : intval($content['parent']),
  27.             'test_url'         =>  emptyempty($content['test_url']) ? NULL : $content['test_url'],
  28.         );

4-3、select函数里添加查询新字段:

  1. /**
  2.      * 获取查询对象
  3.      * @access public
  4.      * @return Typecho_Db_Query
  5.      */
  6.     public function select(){
  7.         return $this->db->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId',
  8.         'table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order',
  9.         'table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed',
  10.         'table.contents.parent','table.contents.test_url')->from('table.contents');
  11.     }

/tmp/phpvIoFbj

方法二:在主题设置中添加代码

只需在主题目录下function.php中添加如下代码即可完成,相对比较简单。

  1. function themeFields($layout) {
  2. $temp = new Typecho_Widget_Helper_Form_Element_Text('temp', NULL, NULL, _t('测试自定义字段'), _t('测试自定义字段描述'));
  3. $layout->addItem($temp);
  4. }

方法三:使用js实现 在开发typecho插件时,可以在激活方法中添加

  1. Typecho_Plugin::factory('admin/write-post.php')->option = array('Temp_Plugin', 'addField');

之后添加如下方法即可,具体代码请自行修改。

  1. public static function addField(){
  2. if($('[name="fieldNames[]"]').last().val()!=""){
  3.                     var html = '<tr><td><input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" class="text-s w-100"></td>'
  4.                             + '<td><select name="fieldTypes[]" id="">'
  5.                             + '<option value="str"><?php _e('字符'); ?></option>'
  6.                             + '<option value="int"><?php _e('整数'); ?></option>'
  7.                             + '<option value="float"><?php _e('小数'); ?></option>'
  8.                             + '</select></td>'
  9.                             + '<td><textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" class="text-s w-100" rows="2"></textarea></td>'
  10.                             + '<td><button type="button" class="btn btn-xs"><?php _e('删除'); ?></button></td></tr>',
  11.                         el = $(html).hide().appendTo('#custom-field table tbody').fadeIn();
  12.                     attachDeleteEvent(el);
  13.                 }
  14.                 $('[name="fieldTypes[]"]').last().find("option[value='str']").attr("selected",true).siblings().remove();
  15.                 $('[name="fieldNames[]"]').last().attr('value', 'thumbUrl').attr('readonly','readonly');
  16.                 $('[name="fieldValues[]"]').last().attr('placeholder','文章缩略图,推荐尺寸:700px*220px');
  17.                 var html = '<tr><td><input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" class="text-s w-100"></td>'
  18.                         + '<td><select name="fieldTypes[]" id="">'
  19.                         + '<option value="str"><?php _e('字符'); ?></option>'
  20.                         + '<option value="int"><?php _e('整数'); ?></option>'
  21.                         + '<option value="float"><?php _e('小数'); ?></option>'
  22.                         + '</select></td>'
  23.                         + '<td><textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" class="text-s w-100" rows="2"></textarea></td>'
  24.                         + '<td><button type="button" class="btn btn-xs"><?php _e('删除'); ?></button></td></tr>',
  25.                     el = $(html).hide().appendTo('#custom-field table tbody').fadeIn();
  26.                 attachDeleteEvent(el);
  27. }

推荐阅读
  • 以下仅供学习使用以及纪念之用,已过时,将不再继续鼓捣,请知悉。因能力有限,将它们弄出来后修改时都得调试半天,日后随缘上香。任何事物的成长都需要沉淀,不然就会成以下这些一样的结果。继续在另一个条漫长的转型不归路上走着…走着……以下仍然可以站内搜索相关简介:001、DNSP...
  • 主题截图:主题简介:一款Typecho版本的TleWeiboForTypecho电脑/手机版微博主题使用方法:将本主题里的所有文件放在您网站目录的usr/themes内,注意文件夹名字必须为TleWeibo或TleWeiboWap。...
  • 插件截图:插件简介:TleUCenterForTypecho是一个用户中心插件,放置于前台网页的左下角,供用户登陆/管理只用,使用邮箱验证码登陆,可以显示微博列表、文章列表、评论列表,也可以浏览用户个人资料,及发布微博、文章等功能,极大的节省了Typecho主题空间位置。...
  • 插件截图:插件介绍:TleLiveCtrlForTypecho是一个基于Kplayer的直播遥控器插件,也可以叫做KplayerForTypecho插件,支持多平台直播推流,进行积分点播、查询、跳过等功能,支持Payjs微信、支付宝支付,是一个可以24小时直播推流的Type...
  • 模板截图:模板介绍:MomentsForTypecho微信圈页面模板是一款仿微信朋友圈的Typecho页面模板,支持Typecho各个版本,直接添加Typecho独立页面即可使用。...

o p
Ú
>