add custom fields for specific page template in wordpress.
/*Custom filed for specific page templae (sub service page) */
add_action('add_meta_boxes', 'add_product_meta');
function add_product_meta()
{
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'servicepage.php' )
{
add_meta_box(
'product_meta',
'Service Information',
'display_product_information',
'page',
'normal',
'high');
}
}
}
function display_product_information()
{
global $post;
$service_price= get_post_meta($post->ID, 'service_price', true);
?>
<div class="row">
<div class="form-group col-sm-12">
<label for="service price" style="vertical-align:top;">Service Price</label>
<input type="text" name="service_price" class="form-control" value="<?php
if(isset($service_price)) {
echo $service_price;
}
?>" />
</div>
</div>
<?php
}
function service_meta_save($post_id, $post) {
global $post;
update_post_meta($post->ID, 'service_price', $_POST['service_price']);
}
add_action( 'save_post', 'service_meta_save', 1, 2);
add_action('add_meta_boxes', 'add_product_meta');
function add_product_meta()
{
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'servicepage.php' )
{
add_meta_box(
'product_meta',
'Service Information',
'display_product_information',
'page',
'normal',
'high');
}
}
}
function display_product_information()
{
global $post;
$service_price= get_post_meta($post->ID, 'service_price', true);
?>
<div class="row">
<div class="form-group col-sm-12">
<label for="service price" style="vertical-align:top;">Service Price</label>
<input type="text" name="service_price" class="form-control" value="<?php
if(isset($service_price)) {
echo $service_price;
}
?>" />
</div>
</div>
<?php
}
function service_meta_save($post_id, $post) {
global $post;
update_post_meta($post->ID, 'service_price', $_POST['service_price']);
}
add_action( 'save_post', 'service_meta_save', 1, 2);
Comments
Post a Comment