WordPress自定义文章类型

WordPress 里面内置了两种常用的 Post Type :Post(文章)、Page(页面),分别用来展示两种类型的内容,一种是相对动态的文章,另一种是相对固定的页面。 除此之外,WordPress 还内置了其他几种 Post Type 用来展示不同类型的内容。但如果是一个比较复杂的网站或者需求自定义程度比较高,这时候就可以手动自己创建一个 Post Type 来展示某类信息。

//注册自定义文章类型-event
add_action( ‘init’, ‘my_custom_post_event’ );
function my_custom_post_event() {
$labels = array(
‘name’ => _x( ‘大事记’, ‘post type 名称’ ),
‘singular_name’ => _x( ‘event’, ‘post type 单个 item 时的名称,因为英文有复数’ ),
‘add_new’ => _x( ‘新建大事记’, ‘添加新内容的链接名称’ ),
‘add_new_item’ => __( ‘新建一个大事记’ ),
‘edit_item’ => __( ‘编辑大事记’ ),
‘new_item’ => __( ‘新大事记’ ),
‘all_items’ => __( ‘所有大事记’ ),
‘view_item’ => __( ‘查看大事记’ ),
‘search_items’ => __( ‘搜索大事记’ ),
‘not_found’ => __( ‘没有找到有关大事记’ ),
‘not_found_in_trash’ => __( ‘回收站里面没有相关大事记’ ),
‘parent_item_colon’ => ”,
‘menu_name’ => ‘大事记’
);
$args = array(
‘labels’ => $labels,
‘description’ => ‘我们网站的大事记信息’,
‘public’ => true,
‘menu_position’ => 5,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘comments’),
‘has_archive’ => true
);
register_post_type( ‘event’, $args );
}

//注册自定义文章类型-添加分类
function my_taxonomies_event() {
$labels = array(
‘name’ => _x( ‘大事记分类’, ‘taxonomy 名称’ ),
‘singular_name’ => _x( ‘大事记分类’, ‘taxonomy 单数名称’ ),
‘search_items’ => __( ‘搜索大事记分类’ ),
‘all_items’ => __( ‘所有大事记分类’ ),
‘parent_item’ => __( ‘该大事记分类的上级分类’ ),
‘parent_item_colon’ => __( ‘该大事记分类的上级分类:’ ),
‘edit_item’ => __( ‘编辑大事记分类’ ),
‘update_item’ => __( ‘更新大事记分类’ ),
‘add_new_item’ => __( ‘添加新的大事记分类’ ),
‘new_item_name’ => __( ‘新大事记分类’ ),
‘menu_name’ => __( ‘大事记分类’ ),
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true
);
register_taxonomy( ‘event_category’, ‘event’, $args );
}
add_action( ‘init’, ‘my_taxonomies_event’, 0 );

//注册自定义文章类型-添加标签
add_action( ‘init’, ‘create_event_taxonomies’, 0 );
function create_event_taxonomies() {
$labels = array(
‘name’ => ‘大事记标签’, ‘taxonomy general name’,
‘singular_name’ => ‘大事记标签’, ‘taxonomy singular name’,
‘search_items’ => ‘搜索大事记标签’,
‘all_items’ => ‘所有大事记标签’,
‘parent_item’ => ‘Parent Genre’,
‘parent_item_colon’ => ‘Parent Genre:’,
‘edit_item’ => ‘编辑大事记标签’,
‘update_item’ => ‘更新大事记标签’,
‘add_new_item’ => ‘添加新大事记标签’,
‘new_item_name’ => ‘New Genre Name’,
‘menu_name’ => ‘大事记标签’,
);
$args = array(
‘hierarchical’ => false,
‘labels’ => $labels,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
// ‘rewrite’ => array( ‘slug’ => event ),
);
register_taxonomy( ‘event_tag’, ‘event’, $args );
}

//添加自定义文章类型相关字段
add_action(‘add_meta_boxes’, ‘event_time’);//活动时间
add_action(‘add_meta_boxes’, ‘event_name’);//活动结果
add_action(‘add_meta_boxes’, ‘event_link’);//事件链接

function event_time() {
add_meta_box(‘event_time’,’活动时间’, ‘event_time_meta_box’, ‘event’, ‘normal’, ‘high’);
}

function event_name() {
add_meta_box(‘event_name’,’事件结果’, ‘event_name_meta_box’, ‘event’, ‘normal’, ‘high’);
}

function event_link() {
add_meta_box(‘event_link’,’事件链接’, ‘event_link_meta_box’, ‘event’, ‘normal’, ‘high’);
}

 

//定义字段输入框
function event_time_meta_box($post) {
wp_nonce_field(‘event_time_meta_box’, ‘event_time_meta_box_nonce’);
$value = get_post_meta($post->ID, ‘event_time’, true);
echo ‘<label for=”event_time”></label>’;
echo ‘<input type=”text” id=”event_time” name=”event_time” value=”‘.$value.'” size=”50″ />’;
}

function event_name_meta_box($post) {
wp_nonce_field(‘event_name_meta_box’, ‘event_name_meta_box_nonce’);
$value = get_post_meta($post->ID, ‘event_name’, true);
echo ‘<label for=”event_name”></label>’;
echo ‘<input type=”text” id=”event_name” name=”event_name” value=”‘.$value.'” size=”50″ />’;
}

function event_link_meta_box($post) {
wp_nonce_field(‘event_link_meta_box’, ‘event_link_meta_box_nonce’);
$value = get_post_meta($post->ID, ‘event_link’, true);
echo ‘<label for=”event_link”></label>’;
echo ‘<input type=”text” id=”event_link” name=”event_link” value=”‘.$value.'” size=”50″ />’;
}

// function event_link_meta_box($post) {
// wp_nonce_field(‘event_link_meta_box’, ‘event_link_meta_box_nonce’);
// echo ‘<input type=”hidden” name=”event_link” id=”event_link” value=”‘.wp_create_nonce( plugin_basename(__FILE__) ).'” />’;
// echo wp_editor(get_post_meta($post->ID, “content_link”, true), “content_link”, $settings = array(‘wpautop’ => true,’media_buttons’=>false) );
// }

add_action( ‘save_post’, ‘event_time_save_meta_box’ );
add_action( ‘save_post’, ‘event_name_save_meta_box’ );
add_action( ‘save_post’, ‘event_link_save_meta_box’ );

//数据提交检查
function event_time_save_meta_box($post_id){
// 安全检查
// 检查是否发送了一次性隐藏表单内容(判断是否为第三者模拟提交)
if ( ! isset( $_POST[‘event_time_meta_box_nonce’] ) ) {return;}
// 判断隐藏表单的值与之前是否相同
if ( ! wp_verify_nonce( $_POST[‘event_time_meta_box_nonce’], ‘event_time_meta_box’ ) ) {return;}

// 判断该用户是否有权限
if ( ! current_user_can( ‘edit_post’, $post_id ) ) {return;}

// 判断 Meta Box 是否为空
if ( ! isset( $_POST[‘event_time’] ) ) {return;}

$event_time = sanitize_text_field( $_POST[‘event_time’] );
update_post_meta( $post_id, ‘event_time’, $event_time );
}

function event_name_save_meta_box($post_id){
// 安全检查
// 检查是否发送了一次性隐藏表单内容(判断是否为第三者模拟提交)
if ( ! isset( $_POST[‘event_name_meta_box_nonce’] ) ) {return;}
// 判断隐藏表单的值与之前是否相同
if ( ! wp_verify_nonce( $_POST[‘event_name_meta_box_nonce’], ‘event_name_meta_box’ ) ) {return;}

// 判断该用户是否有权限
if ( ! current_user_can( ‘edit_post’, $post_id ) ) {return;}

// 判断 Meta Box 是否为空
if ( ! isset( $_POST[‘event_name’] ) ) {return;}

$event_name = sanitize_text_field( $_POST[‘event_name’] );
update_post_meta( $post_id, ‘event_name’, $event_name );
}

function event_link_save_meta_box($post_id){
// 安全检查
// 检查是否发送了一次性隐藏表单内容(判断是否为第三者模拟提交)
if ( ! isset( $_POST[‘event_link_meta_box_nonce’] ) ) {return;}
// 判断隐藏表单的值与之前是否相同
if ( ! wp_verify_nonce( $_POST[‘event_link_meta_box_nonce’], ‘event_link_meta_box’ ) ) {return;}

// 判断该用户是否有权限
if ( ! current_user_can( ‘edit_post’, $post_id ) ) {return;}

// 判断 Meta Box 是否为空
if ( ! isset( $_POST[‘event_link’] ) ) {return;}

$event_link = $_POST[‘content_link’];
update_post_meta( $post_id, ‘content_link’, $event_link );

}

//在列表显示字段及参数
add_action(“manage_posts_custom_column”, “event_custom_columns”);
add_filter(“manage_edit-event_columns”, “event_edit_columns”);
function event_custom_columns($column){
global $post;
switch ($column) {
case “event_time”:
echo get_post_meta($post->ID, ‘event_time’, true);
break;
case “event_name”:
echo get_post_meta($post->ID, ‘event_name’, true);
break;
case “event_link”:
echo get_post_meta($post->ID, ‘event_link’, true);
}
}
//列表显示字段title
function event_edit_columns($columns){
$columns[‘event_time’] = ‘活动时间’;
$columns[‘event_name’] = ‘活动结果’;
$columns[‘event_link’] = ‘活动链接’;
return $columns;
}

WordPress自定义文章类型 (1)

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址