我添加了自定义帖子类型和字段。但当我输入值并保存时,它们不会显示在Wordpress编辑屏幕中。
<?php
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
$supports = array(\'title\', \'editor\', \'thumbnail\');
register_post_type( \'acme_property\',
array(
\'labels\' => array(
\'name\' => __( \'Properties\' ),
\'singular_name\' => __( \'Property\' ),
\'search_items\' => __( \'Search Properties\' ),
\'not_found\' => __( \'No Properties found\' )
),
\'public\' => true,
\'has_archive\' => true,
\'supports\' => $supports
)
);
}
add_action(\'add_meta_boxes\', \'add_property\');
function add_property(){
add_meta_box("property-meta", "Property Info", "meta_options", "acme_property", "side", "low");
}
function meta_options(){
global $post;
$values = get_post_custom($post->ID);
$Prop_ID = isset( $values[\'property_id\'] ) ? esc_attr( $values[\'property_id\'][0] ) : \'\';
$address = isset( $values[\'address\'] ) ? esc_attr( $values[\'address\'][0] ) : \'\';
?>
<p>
<label>Property #: </label><br/><input name="property_id" value="<?php echo $Prop_ID; ?>" />
</p>
<p>
<label>Address:</label><br/><textarea name="address" cols="22" rows="3" value="<?php echo $address; ?>"></textarea>
</p>
<?php
}
add_action( \'save_post\', \'property_save\' );
function property_save( $post_id )
{
// Bail if we\'re doing an auto save
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
// if our nonce isn\'t there, or we can\'t verify it, bail
if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
// if our current user can\'t edit this post, bail
if( !current_user_can( \'edit_post\' ) ) return;
// now we can actually save the data
$allowed = array(
\'a\' => array( // on allow a tags
\'href\' => array() // and those anchors can only have href attribute
)
);
// Make sure your data is set before trying to save it
if( isset( $_POST[\'property_id\'] ) )
update_post_meta( $post_id, \'property_id\', wp_kses( $_POST[\'property_id\'], $allowed ) );
if( isset( $_POST[\'address\'] ) )
update_post_meta( $post_id, \'address\', esc_attr( $_POST[\'address\'] ) );
}