当前位置: > 教程 >

使用Typecho二次开发实现网址导航功能并去掉文章地址的index.php

  • 教程
  • 2020-02-04 14:40
这几天把 堕落了网址导航 的后台程序换成了 Typecho 并进行一点点二次开发以实现网址导航的一些功能。
Typecho 是一个轻量级的PHP博客程序,后台界面是我喜欢的那种,代码也很简洁易读,速度与 WordPress 相比,一个是法拉力,一个是牛车。
 
主要实现的界面如下图:



下面说一下二次开发的过程。
 
1、在数据库contents表中增加字段:
alter table typecho_contents add column sort int(20) default 0 comment '排序';
alter table typecho_contents add column showurl VARCHAR(200) default null comment '显示链接';
alter table typecho_contents add column jumpurl VARCHAR(200) default null comment '跳转链接';
alter table typecho_contents add column `describe` VARCHAR(200) default null comment '站点描述';
alter table typecho_contents add column sitelogo VARCHAR(200) default null comment '站点标志';
 
2、修改\admin\write-post.php文件,在“网址缩略名”下面(大约39行)增加:

                    <p>
                       <input type="text" name="describe" class="w-100" placeholder="<?php _e('站点描述'); ?>" value="<?php $post->describe(); ?>"/>
                    </p>
                    <p>
                       <input type="text" name="showurl" class="w-40" placeholder="<?php _e('显示链接'); ?>" value="<?php $post->showurl(); ?>"/>
                       <input type="text" name="jumpurl" class="w-40" placeholder="<?php _e('跳转链接'); ?>" value="<?php $post->jumpurl(); ?>"/>
                    </p>
                    <p>
                       <input type="text" name="sitelogo" class="w-40" placeholder="<?php _e('站点标志'); ?>" value="<?php $post->sitelogo(); ?>"/>
                       <input type="text" name="sort" class="w-40" placeholder="<?php _e('排序'); ?>" value="<?php $post->sort(); ?>"/>
                   </p>
 
3、修改\var\Widget\Contents\Post\Edit.php文件。
搜索 public function writePost(),在下面添加'sort', 'showurl', 'jumpurl', 'describe', 'sitelogo',示例:

public function writePost()
    {
        $contents = $this->request->from('password', 'allowcomment',
            'allowPing', 'allowFeed', 'slug', 'tags', 'text', 'visibility', 'sort', 'showurl', 'jumpurl', 'describe', 'sitelogo');
 
4、修改\var\Widget\Abstract\Contents.php文件。
搜索 public function select() 参照如下添加'table.contents.sort','table.contents.showurl','table.contents.jumpurl','table.contents.describe','table.contents.sitelogo',示例:

    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.sort','table.contents.showurl','table.contents.jumpurl','table.contents.describe','table.contents.sitelogo')->from('table.contents');
    }
 
搜索 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']),
            // 以下是我新增加的五个字段及对应的参数
            'sort'        =>  empty($content['sort']) ? NULL : $content['sort'],
            'showurl'        =>  empty($content['showurl']) ? NULL : $content['showurl'],
            'jumpurl'        =>  empty($content['jumpurl']) ? NULL : $content['jumpurl'],
            'describe'        =>  empty($content['describe']) ? NULL : $content['describe'],
            'sitelogo'        =>  empty($content['sitelogo']) ? NULL : $content['sitelogo']
     );
     
搜索 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']),
            // 以下是新增加的五个字段及对应的参数
            'sort'        =>  empty($content['sort']) ? NULL : $content['sort'],
            'showurl'        =>  empty($content['showurl']) ? NULL : $content['showurl'],
            'jumpurl'        =>  empty($content['jumpurl']) ? NULL : $content['jumpurl'],
            'describe'        =>  empty($content['describe']) ? NULL : $content['describe'],
            'sitelogo'        =>  empty($content['sitelogo']) ? NULL : $content['sitelogo']
     );
     
5、修改/var/Widget/Archive.php文件,实现自定义排序功能,数字越大越靠前:

        /** 仅输出文章 */
        $this->_countSql = clone $select;

        $select->order('table.contents.sort', Typecho_Db::SORT_DESC)
     
 6、修改/admin/manage-posts.php文件,后台管理文章页面显示排序字段
搜索<table class="typecho-list-table">参照如下添加:

                        <colgroup>
                            <col width="20"/>
                            <col width="6%"/>
                            <col width="35%"/>
                            <col width=""/>
                            <col width="18%"/>
                            <col width=""/><!-- 增加一行 -->
                            <col width="15%"/>
                        </colgroup>
                        <thead>
                            <tr>
                                <th> </th>
                                <th> </th>
                                <th><?php _e('标题'); ?></th>
                                <th><?php _e('作者'); ?></th>
                                <th><?php _e('分类'); ?></th>
                                <th><?php _e('排序'); ?></th><!-- 增加一行 -->
                                <th><?php _e('日期'); ?></th>
                            </tr>
                        </thead>


搜索<?php endforeach; ?>参照如下添加:

                                <?php endforeach; ?>
                                </td>
                                 <td><?php echo $posts->sort; ?></td><!-- 增加一行 -->
                               <td>
                                <?php if ($posts->hasSaved): ?>

7、去掉文章路径中的/index.php/(主机必须要支持伪静态!!!)
在后台-设置-永久链接,是否使用地址重写功能,这里要选择“启用”,如果主机支持伪静态但是提示“写功能检测失败, 请检查你的服务器设置”,继续开启后文章页面就会出现404打不开的问题,可以在根目录创建.htaccess文件完美解决。

 
.htaccess文件内容为:
 
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
 

猜你喜欢

最新评论

说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!