这是我的代码
// Our custom post type function
function create_posttype_ticket() {
register_post_type( 'ticket',
array(
'labels' => array(
'name' => __( 'Ticket' ),
'singular_name' => __( 'Ticket' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'thumbnail', 'author'),
'rewrite' => array('slug' => 'ticket'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_ticket' );
我想在自定义帖子类型中添加更多标签/文本框
电影:发行日期
电影:演员
任何想法
答案 0 :(得分:0)
在function.php或插件文件中添加以下代码。
此代码将在您的自定义帖子类型中创建一些自定义字段。
function movie_detail_meta_box(){
add_meta_box(
'movie_detail_box_id', // Unique ID
'Movie Details', // Box title
'render_movie_detail_meta_box_html',
'ticket', // Post type
'normal',
'low'
);
}
add_action('add_meta_boxes', 'movie_detail_meta_box');
function render_movie_detail_meta_box_html($post){
$meta = get_post_meta( $post->ID );
wp_nonce_field( 'movie_detail_metabox', 'movie_detail_metabox_nonce' );
$val_movie_name = ( isset( $meta['movie_name'][0] ) && '' !== $meta['movie_name'][0] ) ? $meta['movie_name'][0] : '';
$val_movie_release = ( isset( $meta['movie_release'][0] ) && '' !== $meta['movie_release'][0] ) ? $meta['movie_release'][0] : '';
$val_movie_cast = ( isset( $meta['movie_cast'][0] ) && '' !== $meta['movie_cast'][0] ) ? $meta['movie_cast'][0] : '';
// print_r("<p>val_autoplayTime-".$val_autoplayTime."<p/>");
echo "<div class='movie_meta_fields_container'>";
echo '<p><label for="movie_name"><b>Movie Name: </b></label><input type="text" name="movie_name" id="movie_name" style="width:100%;" value="'. esc_attr( $val_movie_name ) .'"></p>';
echo '<p><label for="movie_release"><b>Movie Release: </b></label><input type="text" name="movie_release" id="movie_release" style="width:100%;" value="'. esc_attr( $val_movie_release ) .'"></p>';
echo '<p><label for="movie_cast"><b>Movie Cast: </b></label><input type="text" name="movie_cast" id="movie_cast" style="width:100%;" value="'. esc_attr( $val_movie_cast ) .'"></p>';
echo "</div>";
}
function movie_detail_save_metadata($post_id){
// Check if our nonce is set.
if ( ! isset( $_POST['movie_detail_metabox_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['movie_detail_metabox_nonce'] ), 'movie_detail_metabox' ) ) { // Input var okay.
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// OK, it's safe for us to save the data now.
// Make sure that it is set.
if ( !isset($_POST['movie_name']) || !isset($_POST['movie_release']) || !isset($_POST['movie_cast']) ) {
return;
}
$fields = ['movie_name','movie_release','movie_cast'];
foreach ($fields as $field) {
if (array_key_exists($field, $_POST)){
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
}
}
}
add_action('save_post', 'movie_detail_save_metadata');
请随时询问是否卡在任何地方。