WordPress创建自定义文章类型 CPT (custom post type)

注册自定义文章类型,代码如下:

add_action('init', 'my_custom_post_product');
function my_custom_post_product() {
    $labels = array(
        'name'               => _x('产品中心', 'post type 名称'),
        'singular_name'      => _x('产品', '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,
        'menu_icon'     => 'dashicons-money-alt',
        // 支持的功能,还可以添加:'excerpt', ,  'comments'
        'supports'      => array('title', 'editor', 'thumbnail'),
        /*这个设置为true,
        才能在前台 single-[post_type].php 和 archive-[post_type].php 显示列表*/
        'has_archive'   => true

    );
    // 注册文章类型,第一个参数为类型名称,即post type name
    register_post_type('product', $args);
}

为新注册的类型,添加分类功能,代码如下:

/**
 * 为产品 post type 添加分类功能
 */
add_action('init', 'my_taxonomies_product', 0);
function my_taxonomies_product() {
    $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,
    );
    /** 注册分类,第一个参数为分类法的名字,即taxonomy
    第二个参数为该分类属于哪个文章类型 */
    register_taxonomy('product_category', 'product', $args);
}


为新注册的文章类型添加额外的输入框,即 meta box

/**
 * 为产品发布类型 添加自定义 Meta Box
 * 功能特征 meta box
 */
add_action('add_meta_boxes', 'product_gongneng');
function product_gongneng() {
    // add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
    add_meta_box(
        'product_gongneng',                 // id
        '功能特征',                          // title        
        'product_gongneng_meta_box',        // callback
        'product',                          // post_type
        'normal',                           // context
        'low'                               // priority
    );
}

/**
 * 为meta box 添加html表单
 */
function product_gongneng_meta_box($post) {
    // 创建临时隐藏表单,为了安全
    wp_nonce_field('product_gongneng_meta_box', 'product_gongneng_meta_box_nonce');

    // 获取之前存储的值
    $field_value = get_post_meta($post->ID, '_product_gongneng', false);
?>
    <p>
        <label for="_product_gongneng"></label>
        <?php
        $settings = array(
            'quicktags' => array('buttons' => 'em,strong,link',),
            'quicktags' => true,
            'tinymce' => true,
            'editor_height' => 100, // In pixels, takes precedence and has no default value
            'textarea_rows' => 20,  // Has no visible effect if editor_height is set, default is 20
        );

        wp_editor(html_entity_decode($field_value[0]), '_product_gongneng', $settings);
        ?>
    </p>
<?php

}


// 保存meta_box
add_action('save_post', 'product_gongneng_save_meta_box');
function product_gongneng_save_meta_box($post_id) {
    // 安全检查
    // 检查是否发送了一次性隐藏表单内容(判断是否为第三者模拟提交)
    if (!isset($_POST['product_gongneng_meta_box_nonce'])) {
        echo 'a11111';
        exit();
        return;
    }

    // 判断隐藏表单的值与之前是否相同
    if (!wp_verify_nonce($_POST['product_gongneng_meta_box_nonce'], 'product_gongneng_meta_box')) {
        echo '222';
        exit();
        return;
    }

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


    // 判断 Meta Box 是否为空
    if (!isset($_POST['_product_gongneng'])) {
        echo '444';
        exit();
        return;
    }

    // 使用 wpautop 函数,是因为表单中 使用了富文本编辑器,要对文本进行处理
    $gongeng = htmlentities(wpautop($_POST['_product_gongneng']));
    $_product_gongneng = sanitize_text_field($gongeng);
    // 把字段值保存到数据库
    update_post_meta($post_id, '_product_gongneng', $_product_gongneng);
}
本文为“老吴笔记”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注