admin管理员组文章数量:1123847
how to make meta custom fields in custom post type name jobs_board_cpt , I am using boilerplate ?
public function jobs_board_cpt() {
$labels = array(
'name' => _x( 'All Jobs', 'Post Type General Name', 'twentyfive' ),
'singular_name' => _x( 'Job Board', 'Post Type Singular Name', 'twentyfive' ),
'menu_name' => __( 'Jobs Board', 'twentyfive' ),
'name_admin_bar' => __( 'Jobs Board', 'twentyfive' ),
'archives' => __( 'Jobs Board Archives', 'twentyfive' ),
'attributes' => __( 'Jobs Board Attributes', 'twentyfive' ),
'parent_item_colon' => __( 'Parent Job Board:', 'twentyfive' ),
'all_items' => __( 'All Jobs', 'twentyfive' ),
'add_new_item' => __( 'Add New Job', 'twentyfive' ),
'add_new' => __( 'Add New Job', 'twentyfive' ),
'new_item' => __( 'New Job ', 'twentyfive' ),
'edit_item' => __( 'Edit Job', 'twentyfive' ),
'update_item' => __( 'Update Job', 'twentyfive' ),
'view_item' => __( 'View Job', 'twentyfive' ),
'view_items' => __( 'View Jobs', 'twentyfive' ),
'search_items' => __( 'Search Jobs', 'twentyfive' ),
'not_found' => __( 'Jobs Not found', 'twentyfive' ),
'not_found_in_trash' => __( 'Jobs Not found in Trash', 'twentyfive' ),
'featured_image' => __( 'Featured Image Jobs Board', 'twentyfive' ),
'set_featured_image' => __( 'Set featured Job image', 'twentyfive' ),
'remove_featured_image' => __( 'Remove featured image Job ', 'twentyfive' ),
'use_featured_image' => __( 'Use as featured image Jobs Board', 'twentyfive' ),
'insert_into_item' => __( 'Insert into Jobs Board', 'twentyfive' ),
'uploaded_to_this_item' => __( 'Uploaded to this Jobs Board', 'twentyfive' ),
'items_list' => __( 'Jobs list', 'twentyfive' ),
'items_list_navigation' => __( 'Jobs list navigation', 'twentyfive' ),
'filter_items_list' => __( 'Filter Jobs list', 'twentyfive' ),
);
$args = array(
'label' => __( 'Job Board', 'twentyfive' ),
'description' => __( 'This is Jobs Board custom type', 'twentyfive' ),
'labels' => $labels,
'supports' => array( 'title', 'editor','custom-fields', 'thumbnail', 'post-formats' ),
//'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-shortcode',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'jobs_board_cpt', $args );
}
// Add meta boxes for jobs_board custom post type
//add_action('add_meta_boxes', 'add_jobs_board_meta_boxes');
public function add_jobs_board_meta_boxes() {
add_meta_box(
'job_salary_meta_box',
'Salary',
'render_job_salary_meta_box',
'jobs_board_cpt',
'normal',
'high'
);
add_meta_box(
'job_location_meta_box',
'Location',
'render_job_location_meta_box',
'jobs_board_cpt',
'normal',
'high'
);
}
// Render job salary meta box
public function render_job_salary_meta_box($post) {
$salary = get_post_meta($post->ID, 'job_salary', true);
?>
<label for="job_salary">Salary:</label>
<input type="text" id="job_salary" name="job_salary" value="<?php echo esc_attr($salary); ?>">
<?php
}
// Render job location meta box
public function render_job_location_meta_box($post) {
$location = get_post_meta($post->ID, 'job_location', true);
?>
<label for="job_location">Location:</label>
<input type="text" id="job_location" name="job_location" value="<?php echo esc_attr($location); ?>">
<?php
}
// Save job board meta data
//add_action('save_post', 'save_jobs_board_meta_data');
public function save_jobs_board_meta_data($post_id) {
// Check if nonce is set
if (!isset($_POST['nonce_field'])) {
return;
}
// Verify nonce
if (!wp_verify_nonce($_POST['nonce_field'], 'nonce_action')) {
return;
}
// Check if this is an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check if user has permissions to save data
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Save meta data for salary
if (isset($_POST['job_salary'])) {
update_post_meta($post_id, 'job_salary', sanitize_text_field($_POST['job_salary']));
}
// Save meta data for location
if (isset($_POST['job_location'])) {
update_post_meta($post_id, 'job_location', sanitize_text_field($_POST['job_location']));
}
}
本文标签: custom post type with metabox custom fields
版权声明:本文标题:custom post type with metabox custom fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736599646a1945192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论