开源项目 custom-post-type-permalinks
使用教程
1. 项目的目录结构及介绍
custom-post-type-permalinks/
├── assets/
│ ├── css/
│ ├── js/
│ └── img/
├── includes/
│ ├── class-custom-post-type-permalinks.php
│ ├── class-custom-post-type-permalinks-admin.php
│ └── class-custom-post-type-permalinks-util.php
├── languages/
│ └── custom-post-type-permalinks.pot
├── custom-post-type-permalinks.php
└── readme.txt
- assets/: 包含项目的静态资源文件,如CSS、JavaScript和图片。
- includes/: 包含项目的主要功能类文件。
class-custom-post-type-permalinks.php
: 主类文件,处理自定义文章类型的固定链接。class-custom-post-type-permalinks-admin.php
: 管理后台类文件,处理后台设置。class-custom-post-type-permalinks-util.php
: 工具类文件,提供辅助功能。
- languages/: 包含项目的语言文件,支持多语言。
- custom-post-type-permalinks.php: 项目的主启动文件。
- readme.txt: 项目的说明文档。
2. 项目的启动文件介绍
custom-post-type-permalinks.php
是项目的主启动文件,负责初始化插件并加载必要的文件。以下是该文件的主要内容:
<?php
/*
Plugin Name: Custom Post Type Permalinks
Plugin URI: https://github.com/torounit/custom-post-type-permalinks
Description: Add support of permalinks to custom post types.
Version: 1.0
Author: Toro_Unit
Author URI: https://torounit.com
Text Domain: custom-post-type-permalinks
Domain Path: /languages
*/
require_once( dirname( __FILE__ ) . '/includes/class-custom-post-type-permalinks.php' );
require_once( dirname( __FILE__ ) . '/includes/class-custom-post-type-permalinks-admin.php' );
require_once( dirname( __FILE__ ) . '/includes/class-custom-post-type-permalinks-util.php' );
$custom_post_type_permalinks = new Custom_Post_Type_Permalinks();
$custom_post_type_permalinks->init();
- Plugin Name: 插件的名称。
- Plugin URI: 插件的官方网站。
- Description: 插件的描述。
- Version: 插件的版本号。
- Author: 插件的作者。
- Text Domain: 插件的文本域,用于多语言支持。
- Domain Path: 语言文件的路径。
3. 项目的配置文件介绍
项目的配置主要通过 class-custom-post-type-permalinks.php
和 class-custom-post-type-permalinks-admin.php
文件进行。以下是 class-custom-post-type-permalinks.php
的部分代码:
class Custom_Post_Type_Permalinks {
public function init() {
add_action( 'init', array( $this, 'rewrite_rules' ) );
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 );
add_filter( 'term_link', array( $this, 'term_link' ), 10, 3 );
}
public function rewrite_rules() {
// 添加自定义重写规则
}
public function post_type_link( $link, $post ) {
// 修改自定义文章类型的链接
}
public function term_link( $termlink, $term, $taxonomy ) {
// 修改自定义分类法的链接
}
}
- init(): 初始化方法,添加必要的动作和过滤器。
- rewrite_rules(): 添加自定义重写规则。
- post_type_link(): 修改自定义文章类型的链接。
- term_link(): 修改自定义分类法的链接。
通过这些配置文件,可以灵活地修改和扩展自定义文章类型和分类法的固定链接。