Typecho增加固定字段的解决方法!

今天因为一个需求,想在文章表里增加一个img_url的缩略图,就翻遍了网上的教程,最终找到了一个详细的步骤,今天予以记录。

1、添加字段

打开mysql数据库,在 typecho_contents 表中新建一个 test_url 字段,类型可为字符串。

2、write-post.php

在后台目录对write-post.php 进行修改,我将位置放到了文章内容下面

					<p>
   缩略图设置:<input type="text" name="img_url" value="<?php $post->test_url(); ?>" style="width:100%"/>

                    </p>
                    
                    <?php include 'custom-fields.php'; ?>

3、Edit.php

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

发布新文章

/**
    * 发布文章
    *
    * @access public
    * @return void
    */
public function writePost()
{
    $contents = $this->request->from('password', 'allowComment',
        'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility', 'img_url');

4、Contents.php

Widget/Abstract/Contents.php 

insert函数 添加新参数:

/**
    * 插入内容
    *
    * @access public
    * @param array $content 内容数组
    * @return integer
    */
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']),
        'img_url'      =>  empty($content['img_url']) ? NULL : $content['img_url']
    );

update函数 里构建更新结构加入新字段:

/**
    * 更新内容
    *
    * @access public
    * @param array $content 内容数组
    * @param Typecho_Db_Query $condition 更新条件
    * @return integer
    */
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']),
        'img_url'      =>  empty($content['img_url']) ? NULL : $content['img_url']
    );

select函数 里添加查询新字段:

/**
    * 获取查询对象
    *
    * @access public
    * @return Typecho_Db_Query
    */
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.img_url')->from('table.contents');
}

5、使用方法

使用方法:

主题下的 post.php 添加如下即可

<?php $this->img_url(); ?>

6、下面内容对你们没用,是我自己记录的mysql命令,不用扫码观看。


价格: 100.00 元
VIP会员免费终身会员免费
温馨提示:登录付款后可永久阅读隐藏内容。 付费可读