我对WordPress开发有点陌生,所以很沮丧——我创建了一些自定义的帖子类型,并试图向其中添加自定义字段。由于某些原因,当我尝试添加新帖子时,字段将不会显示!!!这是我的代码——它位于自定义post类型文件中。
<?php
/**
* Plugin Name: Art Beat
* Plugin URI: #
* Version: 1.0
* Author: bjorkland
* Author URI: http://localhost:8888/wp-admin/plugins.php?plugin_status=all&paged=1&s
* Description: A custom post type
* License: GPL2
*/
class ArtBeat {
function __construct() {
add_action( \'init\', array( $this, \'register_custom_post_type\' ) );
}
function register_custom_post_type() {
register_post_type( \'artbeat\', array(
\'labels\' => array(
\'name\' => _x( \'Art Beats\', \'post type general name\', \'artbeat\' ),
\'singular_name\' => _x( \'Art Beat\', \'post type singular name\', \'artbeat\' ),
\'menu_name\' => _x( \'Art Beat\', \'admin menu\', \'artbeat\' ),
\'name_admin_bar\' => _x( \'Art Beat\', \'add new on admin bar\', \'artbeat\' ),
\'add_new\' => _x( \'Add New Art Beat\', \'art\', \'artbeat\' ),
\'add_new_item\' => __( \'Add New Art Beat\', \'artbeat\' ),
\'new_item\' => __( \'New Art Beat\', \'artbeat\' ),
\'edit_item\' => __( \'Edit Art Beat\', \'artbeat\' ),
\'view_item\' => __( \'View Art Beat\', \'artbeat\' ),
\'all_items\' => __( \'All Art Beats\', \'artbeat\' ),
\'search_items\' => __( \'Search Art Beats\', \'artbeat\' ),
\'parent_item_colon\' => __( \'Parent Art Beat:\', \'artbeat\' ),
\'not_found\' => __( \'No Art Beat found.\', \'artbeat\' ),
\'not_found_in_trash\' => __( \'No Art Beat found in Trash.\', \'artbeat\' ),
),
// Frontend
\'has_archive\' => false,
\'public\' => false,
\'publicly_queryable\' => false,
// Admin
\'capability_type\' => \'post\',
\'menu_icon\' => \'dashicons-businessman\',
\'menu_position\' => 10,
\'query_var\' => true,
\'show_in_menu\' => true,
\'show_ui\' => true,
\'supports\' => array(
\'title\',
\'author\',
\'comments\',
),
) );
}
}
$ArtBeat = new ArtBeat;
function add_artbeat_meta_boxes() {
add_meta_box("artbeat_contact_meta", "Contact Details", "add_contact_details_artbeat_meta_box", "artbeats", "normal", "low");
}
function add_contact_details_artbeat_meta_box()
{
global $post;
$custom = get_post_custom( $post->ID );
?>
<style>.width99 {width:99%;}</style>
<p>
<label>Date:</label><br />
<textarea rows="5" name="date" class="width99"><?= @$custom["date"][0] ?></textarea>
</p>
<p>
<label>Museum:</label><br />
<input type="text" name="museum" value="<?= @$custom["museum"][0] ?>" class="width99" />
</p>
<?php
}
function save_artbeat_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "date", @$_POST["date"]);
update_post_meta($post->ID, "museum", @$_POST["museum"]);
}
}
add_action( \'admin_init\', \'add_artbeat_meta_boxes\' );
add_action( \'save_post\', \'save_artbeat_custom_fields\' );