我试图添加一个自定义帖子类型和一个自定义元框。我已经做到了。但我面临的另一个问题是。当我在自定义元数据库中输入任何值时,我创建了它,但没有将数据保存在后端。它在前端工作得很好。
现在,因为每当我更新帖子时,它不会在后端保留值,所以每次我都必须输入值。
以下是我迄今为止所做的工作:
function news_post_type() {
$labels = array(
\'name\' => _x( \'News\', \'post type general name\' ),
\'singular_name\' => _x( \'News\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => __( \'Add New\' ),
\'edit_item\' => __( \'Edit News\' ),
\'new_item\' => __( \'New News\' ),
\'all_items\' => __( \'All News\' ),
\'view_item\' => __( \'View News\' ),
\'search_items\' => __( \'Search News\' ),
\'not_found\' => __( \'Not found\' ),
\'not_found_in_trash\' => __( \'Not found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'News\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'This section contains the News section of the site\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\'),
\'has_archive\' => true,
);
register_post_type( \'news\', $args );
}
add_action( \'init\', \'news_post_type\' );
对于metabox:
add_action( \'add_meta_boxes\', \'news_date_box\' );
function news_date_box() {
add_meta_box(
\'news_date_box\',
__( \'News Date\', \'myplugin_textdomain\' ),
\'news_date\',
\'news\',
\'side\',
\'high\'
);
}
function news_date( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), \'news_date_content_nonce\' );
echo \'<input type="text" id="news_date" name="news_date" placeholder="Enter Date"/>\';
}
add_action( \'save_post\', \'news_date_save\' );
function news_date_save( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST[\'news_date_content_nonce\'], plugin_basename( __FILE__ ) ) )
return;
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
} else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
}
$news_date = $_POST[\'news_date\'];
update_post_meta( $post_id, \'news_date\', $news_date );
}
我搜索了解决方案,但没有找到任何解决方案,最后来到了这里。我可能在上述代码中出错,或者我必须添加其他代码。任何解决这种情况的建议。
提前谢谢。