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');
Custom field for custom post type
in
costum field,
Wordpress
- on Monday, July 17, 2023, , 1:35 AM
- No comments
Labels:
costum field,
Wordpress
Virender Bartwal
I am a digital marketing professional dedicated to assisting businesses in achieving success in the online realm ! With more than 10 yrs of hands-on experience in various areas such as SEO, local SEO, Analytics, and WordPress development, I have acquired a wealth of knowledge and proficiency. Additionally, I maintain this blog where I delve into these subjects, constantly seeking innovative methods to disseminate my expertise among fellow enthusiasts.
Twitter | LinkedIn | Website
Twitter | LinkedIn | Website