Search for the Article

Custom field for custom post type

  
	

function custom_add_template_field() {
    add_meta_box(
        'industry_template_field', // Meta box ID
        'Select Template', // Meta box title
        'custom_template_field_callback', // Callback function to render the field
        'industry', // Post type where the meta box should appear (replace 'industry' with your custom post type name)
        'side', // Meta box position (optional: 'normal', 'advanced', or 'side')
        'default' // Meta box priority (optional: 'high', 'core', 'default', or 'low')
    );
}
add_action('add_meta_boxes', 'custom_add_template_field');



function custom_template_field_callback($post) {
    // Get the current value of the field (if it exists)
    $current_template = get_post_meta($post->ID, 'industry_template', true);

    // Define your custom template choices here
    $template_choices = array(
        'template-1' => 'Template 1',
        'template-2' => 'Template 2',
        'template-3' => 'Template 3',
		'industry' => 'Industry Page Template',
        // Add more templates and their corresponding names as choices.
    );

    // Output the HTML for the select dropdown
    echo '';
    echo '';
}


function custom_save_template_field($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    if (isset($_POST['industry_template'])) {
        update_post_meta($post_id, 'industry_template', sanitize_text_field($_POST['industry_template']));
    }
}
add_action('save_post', 'custom_save_template_field');