使用元框创建自定义帖子类型标题名称和年龄 [英] Create Custom post type with meta boxes Title name and age
本文介绍了使用元框创建自定义帖子类型标题名称和年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要在Word-press中创建带有三个标题,名称,代码的自定义帖子类型。我已经熟悉创建自定义帖子类型,但是我不希望图像,描述或其他任何内容。我只需要上面提到的三个领域。谁能指导我实现此功能。
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.
推荐答案
// 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.
这篇关于使用元框创建自定义帖子类型标题名称和年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文