WordPress

rozwój wtyczki – niestandardowy typ postu z niestandardowymi polami metabox

  • 17 marca, 2024
  • 3 min read
rozwój wtyczki – niestandardowy typ postu z niestandardowymi polami metabox


jak utworzyć niestandardowe pola meta w niestandardowej nazwie typu postu Jobs_board_cpt, używam szablonu? funkcja publiczna 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’); funkcja publiczna 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'
);

}

// Renderuj meta box wynagrodzenia za pracę funkcja publiczna render_job_salary_meta_box($post) { $salary = get_post_meta($post->ID, 'job_salary’, true); ?> Wynagrodzenie: „>

Warto przeczytać!  zapytanie wp - problem z niestandardowym widżetem WP_Query

// Renderuj metabox lokalizacji zadania publiczna funkcja render_job_location_meta_box($post) { $location = get_post_meta($post->ID, 'job_location’, true); ?> Lokalizacja: „>

// Zapisz metadane tablicy ogłoszeń //add_action(’save_post’, 'save_jobs_board_meta_data’); funkcja publiczna save_jobs_board_meta_data($post_id) { // Sprawdź, czy ustawiono wartość nonce if (!isset($_POST[’nonce_field’])) { powrót; }

// 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']));
}

}


Źródło