Typecho 默认的文章页编辑是不能使用(自定义模板)的,以及默认的独立页编辑是不能使用(标签)的,本文档正是开启这两项功能,让文章页可以使用自定义模板,呈现不同的文章类型;让独立页可以使用标签,纳入标签云和标签页。

使用方法

给文章页开启自定义模板功能

打开文件 admin/write-post.php 在适当位置(如:第 120 行)添加以下代码:

<section class="typecho-post-option">
    <label for="template" class="typecho-label"><?php _e('自定义模板'); ?></label>
    <p>
        <select name="template" id="template">
            <option value=""><?php _e('不选择'); ?></option>
            <?php $templates = $post->getTemplates();
            foreach ($templates as $template => $name): ?>
                <option
                    value="<?php echo $template; ?>"<?php if ($template == $post->template): ?> selected="true"<?php endif; ?>><?php echo $name; ?></option>
            <?php endforeach; ?>
        </select>
    </p>
    <p class="description"><?php _e('如果你为此页面选择了一个自定义模板, 系统将按照你选择的模板文件展现它'); ?></p>
</section>

至此,在后台》管理》文章,任意一篇文章的编辑页面中,已经可以看到(自定义模板)的功能选项。但是,当选择模板后发布页面,再次回到编辑页面中,发现(自定义模板)中并不是之前的模板名称,也就是选择模板的操作没能写入数据库中。接着,打开文件 var/Widget/Contents/Post/Edit.php 在第 261 行之下添加模板字段(template)最终如下:

$contents = $this->request->from(
    'password',
    'allowComment',
    'allowPing',
    'allowFeed',
    'slug',
    'tags',
    'text',
    'template', //新增的模板字段
    'visibility'
);

至此,已完成给文章页开启自定义模板功能的改造,文章页也能像独立页那样,拥有更丰富的内容样式输出。

给独立页开启标签功能

打开文件 admin/write-page.php 在适当位置(如:第 91 行)添加以下代码:

<section class="typecho-post-option">
    <label for="token-input-tags" class="typecho-label"><?php _e('标签'); ?></label>
    <p><input id="tags" name="tags" type="text" value="<?php $page->tags(',', false); ?>"
                                  class="w-100 text"/></p>
</section>

至此,在后台》管理》独立页面,任意一篇文章的编辑页面中,已经可以看到(标签)的功能选项。但是,当新增标签发布页面,再次回到编辑页面中,发现(标签)中并没有新增的标签,也就是新增标签的操作没能写入数据库中。接着,打开文件 var/Widget/Contents/Page/Edit.php 在第 73 行之下添加标签字段(tags)最终如下:

 $contents = $this->request->from(
    'text',
    'template',
    'allowComment',
    'allowPing',
    'allowFeed',
    'slug',
    'tags', //新增的标签字段
    'order',
    'visibility'
);

至此,已完成给独立页开启标签功能的改造,但是,在标签页 http(s)://www.example.com/tag/slug 并没有输出该篇独立页的标题信息,接着,打开文件 var/Widget/Archive.php 将第 1984 行代码:

->where('table.contents.type = ?', 'post');

替换为以下代码:

->where('table.contents.type = ? OR table.contents.type = ?', 'post', 'page');

拓展功能

  1. 在完成以上(给文章页开启自定义模板功能)的改造后,将你的主题文件 post.php 更名为其他名称(如 default.php)。
  2. 在该文件(如 default.php)的相同目录位置,新建空白文件 post.php
  3. 在新建的空白文件 post.php 里,录入以下代码,保存上传:

''' <?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>

<?php

if ($this->category == '分类一 slug 缩略名') { // 文章所属第一个分类与之匹配时,则使用指定模板
    $this->need('分类一指定使用的模板文件名称'); // 注意包含 .php 后缀文件格式
} else if ($this->category == '分类二 slug 缩略名') { // 文章所属第一个分类与之匹配时,则使用指定模板
    $this->need('分类二指定使用的模板文件名称'); // 注意包含 .php 后缀文件格式
} else { // 若非以上指定分类的文章,则使用默认模板
    $this->need('default.php'); // default.php 与以上提到的更名后的文件名称保持一致
}

?> 

至此,已完成给指定分类下的所有文章,使用指定模板的改造。需要注意的是,如果想给指定分类下,已使用指定模板的某篇文章,更换为别的模板时,请在文章编辑页中,通过(自定义模板)重新选择其他模板,选择后的模板优先于指定分类使用的指定模板。

End

本文标题: Typecho 文档:开启文章自定义模板和独立页标签功能

本文链接: https://blog.kisenhz.cn/archives/25.html

除非另有说明,本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

声明:转载请注明文章来源。

最后修改:2024 年 12 月 15 日
如果觉得我的文章对你有用,请随意赞赏