本文以浅显易懂的方式,以添加一个产品类型为例,介绍wordpress中自定义发布类型post_type的添加方法。本文主要关注后台部分的添加,前台请参考官方模板的代码,本文尚在不断更新完善中。
国内文章一大抄,百度一下千篇一律,完全都是翻译的官方手册。官方手册我不会自己看啊?
基础知识普及
若对wordpress架构比较了解请略过此段。
Wordpress所有文章都是存储在`wp_posts`表中,这个表里的东西很杂,除了有普通博客的文章,还有附件,通过`post_type`字段来区分。wordpress里面还有一个叫wp_postmeta的表,大白话讲这个表就是存储关联信息的表。每一篇文章,可能都会有几个关联的信息,比如访问量点击数这种,通过post_meta这种方式来存储和获取。
流程
post_type就是自定义发布类型了,这也是对wordpress进行二次开发的第一步。我们知道wp本身是个博客程序,他所有文章甚至附件都存储在一个表中(wp_posts),通过post_type来区分。wp提供了一整套流程,通过这套流程可以方便的创建自己的伪模型、自定义各种字段、方便的在后台添加创建栏目。这里要特别说明一件事,我们创建了新的post_type,实际数据也是在wp_posts表中的,也就是说,我们并没有真正改变表结构,只是通过post_meta这种方式关联了一些信息,就像是新建了字段一样。流程大致为:
- 注册新类型:就是告诉wp,我们要搞一个新的发布类型,叫什么名字等等。
- 添加
MetaBox:后台里面,字段数据添加、修改的展示表格。 - 把
MetaBox和post_meta关联:上一步只是添加表格,数据提交后,把相应的数据写入post_meta。 - 指定这个新类型的分类
- 设置前台列表页和内容页展示的页面
以下以产品product为例,分五部分介绍核心代码,达到快速入门的目的。拓展时可以直接参考官方手册,常规使用下面70%够用。
新建post_type
//添加product类型
function create_product_type() {
register_post_type( 'product',
array(
'labels' => array(
'name' => '产品',
'singular_name' => '单条产品',
'add_new' => '添加产品',
'add_new_item' => '添加产品',
'edit' => '编辑',
'edit_item' => '编辑产品',
'new_item' => '新产品',
'view' => '查看',
'view_item' => '查看产品',
'search_items' => '查找产品',
'not_found' => '没有找到产品信息',
'not_found_in_trash' => '回收站没有产品',
'all_items' => '产品列表',
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( '' ),
'has_archive' => true
)
);
}
add_action( 'init', 'create_product_type' );
register_post_type官方手册:https://codex.wordpress.org/Function_Reference/register_post_type
添加Meta Box和字段的展示
// 添加Meta Box
function add_product_box() {
add_meta_box( 'product_meta_box', //唯一标记
'产品信息', //Meta Box标题
'display_product_meta_box_callback', //回调函数
'product', //关联的自定义文章类型
'normal', //页面显示位置
'high' //显示位置优先级
);
}
add_action( 'add_meta_boxes', 'add_product_box' );
// 添加展示
function display_product_meta_box_callback( $result ) {
// 产品简称
$product_code = esc_html( get_post_meta( $result->ID, 'product_code', true ) );
?>
<table>
<tr>
<td style="width:120px;">产品代码</td>
<td style="width:85%;"><input type="text" style="width:500px;" name="product_code" value="<?php echo $product_code; ?>" /></td>
<?php wp_nonce_field( 'product_save_nonce', 'product_save_nonce' ); //安全提交函数 ?>
</tr>
</table>
<?php
}
示例中只添加一个字段,就是产品简称。
add_meta_box官方手册:https://developer.wordpress.org/reference/functions/add_meta_box/
MetaBox和post_meta关联
function save_product_fields( $post_id, $post ) {
// 判断类型
if ( $post->post_type == 'product' && wp_verify_nonce( $_POST['product_save_nonce'], 'product_save_nonce' ) ) {
// 写入数据
if ( isset( $_POST['product_code'] ) {
update_post_meta( $post_id, 'product_code', $_POST['product_code'] );
}
}
}
add_action( 'save_post', 'save_product_fields', 10, 2 );
添加分类
// 产品自定义分类
function product_taxonomies() {
register_taxonomy(
'product_category', //自定义分类名
'product', //自定义类型
array(
'labels' => array(
'name' => '产品分类',
'add_new_item' => '添加产品分类',
'new_item_name' => '新产品分类',
),
'show_ui' => true, //后台显示
'show_tagcloud' => false, //是否显示标签云
'hierarchical' => true, //控制分类法格式
)
);
}
add_action( 'init', 'product_taxonomies', 0 );
register_taxonomy官方手册:https://developer.wordpress.org/reference/functions/register_taxonomy/
指定前台展示页面
// 指定页面模板
function product_template( $template_path ) {
if ( get_post_type() == 'product' ) {
if ( is_single() ) {
$theme_file = locate_template( array ( 'single-product.php' ) );
$template_path = $theme_file;
} elseif ( is_archive() ) {
$theme_file = locate_template( array ( 'archive-product.php' ) );
$template_path = $theme_file;
}
}
return $template_path;
}
add_filter( 'template_include', 'product_template', 1 );
locate_template官方手册:https://developer.wordpress.org/reference/functions/locate_template/
本文更新完善中,如有错误请批评指正。



