Metabox in Custom Post Type. Part II

In previous post I wrote what is a Custom Post Type and why we need it. This time I show how to add Metabox.

function about_posttype(){
 register_post_type(
  'about',
  array(
   'labels' => array(
   'name' => __( 'About' ),
   'singular_name' => __( 'About' ),
   'all_items' => __( ' All Blocks' ),
   'add_new' => __( 'Add New Block' ),
   'add_new_item' => __( 'Add New Block' ),
   'edit_item' => __( 'Edit Block' ),
   'new_item' => __( 'Add New Block' ),
   'view_item' => __( 'View Block' ),
   'search_items' => __( 'Search Block' ),
   'not_found' => __( 'No projects found' ),
   'not_found_in_trash' => __( 'No projects found in trash' ),
  ),
  'public' => true,
  'capability_type' => 'post',
  'menu_position' => 3,
  'supports' => array('title', 'editor', 'thumbnail'),
  '_builtin' => false,
  'rewrite' => array('slug' => 'about'),
  'register_meta_box_cb' => 'type_of_block'
  )
 );
}
add_action ('init', 'about_posttype');

Metabox  added to Custom Post type.

function type_of_block() {
 add_meta_box('block_type', 'Kind of block', 'block_type', 'about', 'normal', 'default');
}
function block_type() {
 global $post;
 echo '<input ENGINE="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
 wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
 $type = get_post_meta($post->ID, '_type', true);
 $number = get_post_meta($post->ID, '_number', true);
 switch ($type) {
  case 'regular':
   echo '<label class="setting" for ="_number">Number of block </label><input type="text" name="_number" value="' . $number . '" class="code" />&nbsp;
   <input type="radio" name="_type" value="regular" checked> regular&nbsp;&nbsp;
   <input type="radio" name="_type" value="big_pic"> big picture&nbsp;&nbsp;
   <input type="radio" name="_type" value="texture"> texture';
   break;
  case 'big_pic':
   echo '<label class="setting" for ="_number">Number of block </label><input type="text" name="_number" value="' . $number . '" class="code" />&nbsp;
   <input type="radio" name="_type" value="regular"> regular&nbsp;&nbsp;
   <input type="radio" name="_type" value="big_pic" checked> big picture&nbsp;&nbsp;
   <input type="radio" name="_type" value="texture"> texture';
   break;
  case 'texture':
   echo '<label class="setting" for ="_number">Number of block </label><input type="text" name="_number" value="' . $number . '" class="code" />&nbsp;
   <input type="radio" name="_type" value="regular"> regular&nbsp;&nbsp;
   <input type="radio" name="_type" value="big_pic"> big picture&nbsp;&nbsp;
   <input type="radio" name="_type" value="texture" checked> texture';
   break;
  default:
   echo '<label class="setting" for ="_number">Number of block </label><input type="text" name="_number" value="' . $number . '" class="code" />&nbsp;
   <input type="radio" name="_type" value="regular"> regular&nbsp;&nbsp;
   <input type="radio" name="_type" value="big_pic"> big picture&nbsp;&nbsp;
   <input type="radio" name="_type" value="texture"> texture';
  }
}
function save_block_type($post_id, $post) {
 if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
  return $post->ID;
 }
 if ( !current_user_can( 'edit_post', $post->ID ))
 return $post->ID;
 $events_meta['_type'] = $_POST['_type'];
 $events_meta['_number'] = $_POST['_number'];
 foreach ($events_meta as $key => $value) { 
  if( $post->post_type == 'revision' ) return; 
  $value = implode(',', (array)$value); 
  if(get_post_meta($post->ID, $key, FALSE)) { 
   update_post_meta($post->ID, $key, $value);
  } else { 
   add_post_meta($post->ID, $key, $value);
  }
  if(!$value) delete_post_meta($post->ID, $key); 
 }
}
add_action('save_post', 'save_block_type', 1, 2);

In the code above I added three kind of radio as metabox for three type of posts for about page.

I made template about.php:

<?php
/**
 * Template Name: about
 */ 
get_header(); ?>
 <div id="about" class="about" role="about">
<?php
 query_posts(array('post_type' => 'about', 'posts_per_page' => -1));
?>
<?php get_template_part('loop-about');?>
<?php wp_reset_query();?>
 </div>
<?php get_footer("about");?>

And loop file loop-about.php:

<div id="about">
<?php if ( have_posts() ) :
 while ( have_posts() ) : the_post();
 $number = get_post_meta($post->ID, "_number", true);
 $radiogroup = get_post_meta($post->ID, "_type", true);
 $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
 switch ($radiogroup) {
 case "big_pic" : ?>
 <div class="big-picture-block" style="background-image: url(<?php echo $url ?>);">
 <?php the_content(); ?>
 </div>
 <?php break;
 case "texture": ?>
 <div class="texture-block" style="background-image: url(<?php echo $url ?>);">
 <div class="texture-row">
 <?php the_content(); ?>
 </div>
 </div>
 <?php break;
 case "regular" : ?>
 <div class="regular">
 <?php the_content(); ?>
 </div>
 <?php break; ?>
 <?php }
 ?>
 <?php endwhile; ?>
<?php endif; ?>
</div>

It’s all what I did for¬†http://lilyandcat.com/about/.