我有一个标记为“web\\u design”的自定义帖子类型,我希望我的“web\\u design”帖子具有以下自定义字段:
1客户端“-客户端名称
2”exlink’-外部链接datefin’-完成日期
我设法在我的“Web设计”后期管理UI中添加了一个自定义元数据库(标题为“工作信息”),并提供了必要的输入。这是完整的代码。
<?php
//3. Adding Custom Meta Box for Custom Fields
//a. add custom meta box
function work_meta_box( $post ){
//variables
$id = \'work_info\';
$title = \'Work Information\';
$callback = create_work_meta;
$screen = \'web_design\';
$context = \'side\';
$priority = \'high\';
add_meta_box( $id, $title, $callback, $screen, $context, $priority );
}
add_action( \'add_meta_boxes_web_design\', \'work_meta_box\' );
//b. callback function for \'work_info\' custom meta box (see $callback variable)
function create_work_meta( $post ){
wp_nonce_field( basename( __FILE__ ), \'work_meta_box_nonce\' );
//custom fields
$client = get_post_meta( $post->ID, \'client\', true );
$exlink = get_post_meta( $post->ID, \'exlink\', true );
$datefin = get_post_meta( $post->ID, \'datefin\', true );
echo \'<div>
<p>
<label for=\\\'client\\\'>Client:</label>
<br />
<input type=\\\'text\\\' name=\\\'client\\\' value=\\\'\' . $client .\'\\\' />
</p>
<p>
<label for=\\\'exlink\\\'>External Link:</label>
<br />
<input type=\\\'url\\\' name=\\\'exlink\\\' value=\\\'\'. $exlink . \'\\\' />
</p>
<p>
<label for=\\\'datefin\\\'>Date Finished:</label>
<br />
<input type=\\\'date\\\' name=\\\'datefin\\\' value=\\\'\'. $datefin . \'\\\' required />
</p>
</div>\';
}
//c. saves data after submitting/updating custom post
function save_work_meta( $post_id ){
//1. verifies meta box nonce (to prevent CSRF attacks)
if( !isset( $_POST[\'work_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'work_meta_box_nonce\'] ) ){
return;
}
//2. if autosaves
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ){
return;
}
//3. if user\'s not admin
if( !current_user_can( \'edit_post\', $post_id ) ){
return;
} elseif ( !current_user_can ( \'edit_page\', $post_id) ){
return;
}
//4. checks all custom field values (see \'create_work_meta()\' function)
if( isset( $_REQUEST[\'client\'] ) ){
update_post_meta( $post_id, \'client\', sanitize_text_field( $_POST[\'client\'] ) );
}
if( isset( $_REQUEST[\'exlink\'] ) ){
update_post_meta( $post_id, \'exlink\', sanitize_text_field( $_POST[\'exlink\'] ) );
}
if( isset( $_REQUEST[\'datefin\'] ) ){
update_post_meta( $post_id, \'datefin\', sanitize_text_field( $_POST[\'datefin\'] ) );
}
}
add_action( \'save_post_web_design\', \'save_work_meta\', 10, 2 );
?>
下面是创建“web\\U design”帖子类型的附加代码。我使用的设置可能是这将如何工作的线索。
// H. Creating Custom Posts
// 1. \'Web Design\' Posts (label: \'web_design\')
function web_design_init(){
//a. array set for \'$labels\' variable
$labels = array(
\'name\' => __( \'Web Design\' ),
\'add_new\' => __( \'Add New Sample\' ),
\'add_new_item\' => __( \'Web Design Sample\'),
\'edit_item\' => __( \'Edit Sample\' ),
\'new_item\' => __( \'New Sample\' ),
\'search_items\' => __( \'Search Sample\' ),
\'not_found\' => __( \'No Samples Found\' ),
\'not_found_in_trash\' => __( \'No Samples to Recover\' )
);
//b. array set for \'$supports\' variable
$supports = array(
\'title\',
\'editor\',
\'thumbnail\',
\'post-formats\'
);
//c. array set for \'$args\' variable
$args = array(
\'labels\' => $labels, //a.
\'description\' => \'For web design works in portfolio.\',
\'public\' => true,
\'capability_type\' => \'post\', // <---- I want the \'web_design\' posts to have the same capabilities as a default post item.
\'menu_position\' => 5,
\'menu_icon\' => \'dashicons-images-alt\',
\'hierarchal\' => false,
\'has_archive\' => true,
\'supports\' => $supports, //b.
\'taxonomies\' => array(\'post_tag\')
);
//registers \'web_design\' post type
register_post_type( \'web_design\', $args );
}
add_action( \'init\', \'web_design_init\' );