amy-d
4/13/2013 - 12:41 AM

PHP: WP - Custom Post Type

PHP: WP - Custom Post Type

// REGISTER HOMEPAGE CUSTOM TYPE
     add_action('init', 'work_register');
     function work_register() {

          $labels = array(
               'name' => _x('Work', 'post type general name'),
               'singular_name' => _x('Work', 'post type singular name'),
               'add_new' => _x('Add New', 'feature'),
               'add_new_item' => __('Add New Work'),
               'edit_item' => __('Edit Work'),
               'new_item' => __('New Work'),
               'view_item' => __('View Work'),
               'search_items' => __('Search Features'),
               'not_found' =>  __('Nothing found'),
               'not_found_in_trash' => __('Nothing found in Trash'),
               'parent_item_colon' => ''
          );

          $args = array(
               'labels' => $labels,
               'public' => true,
               'publicly_queryable' => true,
               'show_ui' => true,
               'query_var' => true,
               'rewrite' => array("slug" => "property"),
               'capability_type' => 'post',
               'hierarchical' => true,
               'menu_position' => 5,
               'supports' => array('title', 'editor', 'revisions', 'custom-fields', 'thumbnail')
            );

          register_post_type( 'work' , $args );         
         
          // REGISTER WORK CATEGORIES
          register_post_type( 'work' , $args );
         
          register_taxonomy("work_categories", array("work"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true));
     }
    
     // CUSTOM FORM FIELDS FOR STAFFERS PAGE
     add_action("admin_init", "work_init");

     function work_init(){
          add_meta_box("worker_meta", "Details", "work_completed", "work", "normal", "low");
          add_meta_box("work_order_meta", "Display Order", "work_order_completed", "work", "side", "low");
     }

     function work_completed(){
          global $post;
          $custom = get_post_custom($post->ID);
          $work_type = $custom["work_type"][0];
       ?>
          <style type="text/css" media="screen">
               label.custom_meta {float: left; margin-top: 15px;}
               input.custom_meta {border: 1px solid #DFDFDF; width: 75%; margin: 8px 0 8px 8px; border-radius: 3px; background: #FFF; float: right;}
               input[type=number].custom_meta {width: 75%;}
          </style>
          <label class="custom_meta">Type of Work:</label>
          <input name="work_type" value="<?php echo $work_type; ?>" class="custom_meta" /><br clear="both" />
          <?php
     }
    
     function work_order_completed() {
          global $post;
          $custom = get_post_custom($post->ID);
          $work_order = $custom["work_order"][0];
          ?>
          <label class="custom_meta">Order:</label>
          <input name="work_order" type="number" value="<?php echo $work_order; ?>" class="custom_meta" /><br clear="both" />
          <?php
     }
    
     add_action('save_post', 'save_work_details');
     function save_work_details(){
          global $post;
          $custom_meta_fields = array( 'work_order', 'work_type' );
               foreach( $custom_meta_fields as $custom_meta_field ):
                    if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
                         update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
                    endif;
               endforeach;
     }