使用元框创建自定义帖子类型标题名称和年龄

时间:2016-02-09 04:44:17

标签: wordpress custom-post-type

需要在Word-press中创建自定义帖子类型,其中包含三个元框标题,名称,代码。我已经熟悉创建自定义帖子类型但我不想要图像,描述或任何其他。我只需要上面提到的三个领域。任何人都可以指导我实现这一功能。

1 个答案:

答案 0 :(得分:3)

    // Here your  Custom Post Type
    function generate_shows() {

      $labels = array(
        'name'                => _x( 'Shows', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Show', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Shows', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
        'all_items'           => __( 'All Shows', 'text_domain' ),
        'view_item'           => __( 'View Item', 'text_domain' ),
        'add_new_item'        => __( 'Add New Show', 'text_domain' ),
        'add_new'             => __( 'Add New', 'text_domain' ),
        'edit_item'           => __( 'Edit Item', 'text_domain' ),
        'update_item'         => __( 'Update Item', 'text_domain' ),
        'search_items'        => __( 'Search Shows', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' )
      );
      $args = array(
        'label'               => __( 'enk_show', 'text_domain' ),
        'description'         => __( 'An individual ENK Shows', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title'),
        'taxonomies'          => array( 'category', 'post_tag' ),
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'shows', 'with_front' => false ),
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'register_meta_box_cb' => 'add_enk_metaboxes',
        'capability_type'     => 'post'
      );
      register_post_type( 'enk_show', $args );

    }

    // Hook into the 'init' action
    add_action( 'init', 'generate_shows', 0 );

    add_action( 'admin_init', 'my_admin_samplepost' ); //metabox
    function my_admin_samplepost() {
        add_meta_box( 'enk_show', 'Details', 'display_samplepost_meta_box','enk_show', 'normal', 'high' ); // add_meta_box enk_show is post type
    }
    function display_samplepost_meta_box( $samplepost ) {
        ?>
        <h4>Details</h4>
        <table width="100%">
            <tr>
                <td style="width: 25%">Name</td>
                <td><input type="text" style="width:425px;" name="meta[nameee]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'nameee', true ) );?>" />
                </td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input type="text" style="width:425px;" name="meta[ageee]" placeholder="$" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'ageee', true ) );?>" />
                </td>
            </tr>
        </table>
    <?php 
    }
    add_action( 'save_post', 'add_samplepost_fields', 10, 2 );
    function add_samplepost_fields( $samplepost_id, $samplepost ) {
        if ( $samplepost->post_type == 'enk_show' ) { //note please here your post name itherwise your data will not save
            if ( isset( $_POST['meta'] ) ) {
                foreach( $_POST['meta'] as $key => $value ){
                    update_post_meta( $samplepost_id, $key, $value );
                }
            }
        }
    }

仅复制并粘贴此代码。任何问题然后告诉我。