Custom meta box

Paste in functions.php

add_action( ‘add_meta_boxes’, ‘add_contactus_options’ );
add_action( ‘save_post’, ‘dynamic_assign_contactus_options’ );

function add_contactus_options() {
add_meta_box(
‘contactus_options’,
__( ‘Options’, ‘seltop’ ),
‘dynamic_contactus_custom_box’,
‘event’);
}

function dynamic_contactus_custom_box() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), ‘dynamicMeta_noncename’ );
echo ‘<ul>’;
$meeting = get_post_meta($post->ID, “meeting”,true);
$floor = get_post_meta($post->ID, “floor”,true);

echo ‘<li>Møterom: <input type=”text” name=”meeting” value=”‘.$meeting.'”/></li>’;
echo ‘<li>Etasje: <input type=”text” name=”floor” value=”‘.$floor.'”/></li>’;

echo ‘</ul>’;
}

function dynamic_assign_contactus_options( $post_id ) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST[‘dynamicMeta_noncename’] ) )
return;
if ( !wp_verify_nonce( $_POST[‘dynamicMeta_noncename’], plugin_basename( __FILE__ ) ) )
return;
if($_POST[‘meeting’])
{
update_post_meta($post_id,’meeting’,$_POST[‘meeting’]);
}
if($_POST[‘floor’])
{
update_post_meta($post_id,’floor’,$_POST[‘floor’]);
}

}
?>

Display where you want

<?php
$my_query = new WP_Query(‘post_type=Event&posts_per_page=-1’);
while ($my_query->have_posts()) : $my_query->the_post();   ?>
<tr>
<td><?php the_title(); ?></td>
<td><?php echo get_post_meta($post->ID, “meeting”,true); ?></td>
<td  class=”local”><?php echo get_post_meta($post->ID, “floor”,true); ?></td>
</tr>
<?php endwhile;  wp_reset_query();  ?>

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: