Create Meta box and display on single.php

functions.php


/***** Adding custom metabox on post type  *****/

add_action("add_meta_boxes", "listing_meta_box");
function listing_meta_box(){
add_meta_box('contact_info',__( 'Practice Information', 'realestate' ), 'contact_info', 'practice-areas', 'normal', 'low');
}

function contact_info() {
global $post;
$detail1 = get_post_meta($post->ID,'detail1',true)?get_post_meta($post->ID,'detail1',true):'';
$detail2 = get_post_meta($post->ID,'detail2',true)?get_post_meta($post->ID,'detail2',true):'';
$detail3 = get_post_meta($post->ID,'detail3',true)?get_post_meta($post->ID,'detail3',true):'';
$detail4= get_post_meta($post->ID,'detail4',true)?get_post_meta($post->ID,'detail4',true):'';
$detail5= get_post_meta($post->ID,'detail5',true)?get_post_meta($post->ID,'detail5',true):'';
?>

<table class="form-table">
<tr>
<th><label for="name">
<?php _e('Detail 1','realestate'); ?>
</label></th>
<td><input type="text" name="detail1" value="<?php echo $detail1; ?>" class="listing_input" /></td>
</tr>

<tr>
<th><label for="email">
<?php _e('Detail 2','realestate'); ?>
</label></th>
<td><input type="text" name="detail2" value="<?php echo $detail2; ?>" class="listing_input" /></td>
</tr>
<tr>
<th><label for="phone">
<?php _e('Detail 3','realestate'); ?>
</label></th>
<td><input type="text" name="detail3" value="<?php echo $detail3; ?>" class="listing_input" /></td>
</tr>
<tr>
<th><label for="mobile">
<?php _e('Detail 4','realestate'); ?>
</label></th>
<td><input type="text" name="detail4" value="<?php echo $detail4; ?>" class="listing_input" /></td>
</tr>
<tr>
<th><label for="fax">
<?php _e('Detail 5','realestate'); ?>
</label></th>
<td><input type="text" name="detail5" value="<?php echo $detail5; ?>" class="listing_input" /></td>
</tr>

</table>
<?php
}
add_action('save_post', 'realestate_property_save_details');
function realestate_property_save_details(){
global $post;
if(!empty($_POST))
{
//contact info
update_post_meta($post->ID, "detail1", $_POST["detail1"]);
update_post_meta($post->ID, "detail2", $_POST['detail2']);
update_post_meta($post->ID, "detail3", $_POST["detail3"]);
update_post_meta($post->ID, "detail4", $_POST["detail4"]);
update_post_meta($post->ID, "detail5", $_POST["detail5"]);
}
}

Display single.php


if ( is_singular('practice-areas') ) {
$key_1_value = get_post_meta( get_the_ID(), 'detail1', true );
$key_2_value = get_post_meta( get_the_ID(), 'detail2', true );
$key_3_value = get_post_meta( get_the_ID(), 'detail3', true );
$key_4_value = get_post_meta( get_the_ID(), 'detail4', true );
$key_5_value = get_post_meta( get_the_ID(), 'detail5', true );
}

Advertisements

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: