[网页]:自定义post_type管理-时间线功能

13次阅读
没有评论

先来看现有的puock主题的时光圈功能如何实现.

时光圈(Moments)是一个基于WordPress自定义文章类型的微博客功能,采用时间线布局展示动态内容。

现有功能打造

1. 自定义文章类型注册

时光圈通过 register_post_type() 注册名为 moments 的自定义文章类型 moments.php:3-41 :

2. URL重写规则

实现自定义URL格式 moments/{ID}.html moments.php:44-61 :

3. 页面模板实现

模板文件 pages/template-moments.php 负责前端渲染 template-moments.php:1-80 :

4. 缓存系统

使用主题统一的缓存机制,缓存键定义为 PKC_MOMENTS

[网页]:自定义post_type管理-时间线功能

复刻自定义时间线版块

步骤1:创建新的自定义文章类型

// 在 inc/ext/ 目录创建新文件,如 my-timeline.php  
function pk_ext_my_timeline_init() {  
    register_post_type('my_timeline', array(  
        'labels' => array(  
            'name' => '我的时间线',  
            'menu_name' => '我的时间线'  
        ),  
        'public' => true,  
        'supports' => array('title', 'editor', 'author', 'comments'),  
        'rewrite' => array('slug' => 'my-timeline'),  
        'has_archive' => true  
    ));  
}  
add_action('init', 'pk_ext_my_timeline_init');

步骤2:创建页面模板

复制 pages/template-moments.php 为 pages/template-my-timeline.php,修改:

  • 模板名称:Template Name: 我的时间线
  • 查询参数:'post_type' => 'my_timeline'
  • 缓存键:创建新的常量如 PKC_MY_TIMELINE

步骤3:添加缓存支持

在 inc/fun/cache.php 中添加:

const PKC_MY_TIMELINE = 'my_timeline_%s';

在缓存失效函数中添加相应的清理逻辑。

步骤4:创建样式文件

在 assets/style/ 目录创建新的样式文件,或扩展现有样式文件,添加新的样式类如 #page-my-timeline

步骤5:URL重写(可选)

如需自定义URL格式,参考 inc/ext/moments.php 中的重写规则实现。

正文完
 0
评论(没有评论)