Create Custom post type with meta boxes Title name and age

匿名 (未验证) 提交于 2019-12-03 01:40:02

问题:

Need to create custom post type in Word-press with three meta boxes title,name,code.I already familiar with creating custom post type but i don't want to image, description or any other. I just need above mentioned three fields. Can anyone guide me to achieve this functionality.

回答1:

    // 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 );                 }             }         }     } 

Only copy and paste this code. any problem then tell me.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!