Create and Display Metabox

functions.php


add_action( 'add_meta_boxes', 'practice_metabox' );
function practice_metabox(){
add_meta_box( 'practice_info', 'Practice Information', 'practice_detail_func', 'practice-areas', 'normal', 'high' ); //You can also use normal to side
}

function practice_detail_func( $post ){
$values = get_post_custom( $post->ID );
$detail1 = isset( $values['detail1'] ) ? esc_attr( $values['detail1'][0] ) : '';
$detail2 = isset( $values['detail2'] ) ? esc_attr( $values['detail2'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
Detail 1 :-
<input type="text" name="detail1" value="<?php echo esc_attr( $detail1 ); ?>" placeholder="" class="box-size" />
Detail 2 :-
<input type="text" name="detail2" value="<?php echo esc_attr( $detail2 ); ?>" placeholder="" class="box-size" />
<?php
}

add_action( 'save_post', 'practice_meta_box_save' );
function practice_meta_box_save( $post_id )
{
if( isset( $_POST['detail1'] ) ) update_post_meta( $post_id, 'detail1', esc_attr( $_POST['detail1'] ) );
if( isset( $_POST['detail2'] ) ) update_post_meta( $post_id, 'detail2', esc_attr( $_POST['detail2'] ) );
}

display


$args_gallery = array('post_type' => 'imagegallery', 'posts_per_page' => -1);
$loop_gallery = new WP_Query($args_gallery);

while ($loop_gallery->have_posts()) : $loop_gallery->the_post();
$img_url = get_post_meta(get_the_ID(),'img_url',true);
echo '<li> <a target="_blank" href="'.$img_url.'">'. get_the_post_thumbnail().' </a> </li>';
endwhile;

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: