Advertisement
Guest User

WP Meta Box

a guest
Mar 22nd, 2011
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. /**
  2.  * Extend RW_Meta_Box class
  3.  * Add field types: 'taxonomy', 'post_type'
  4.  */
  5. class RW_Meta_Box_Extend extends RW_Meta_Box {
  6.    
  7.     function add_missed_values() {
  8.         parent::add_missed_values();
  9.        
  10.         // add 'multiple' option to taxonomy field with checkbox_list type
  11.         foreach ($this->_meta_box['fields'] as $key => $field) {
  12.             if ('taxonomy' == $field['type'] && 'checkbox_list' == $field['options']['type'] || 'custom_post' == $field['type'] && 'checkbox_list' == $field['options']['type']) {
  13.                 $this->_meta_box['fields'][$key]['multiple'] = true;
  14.             }
  15.         }
  16.     }
  17.    
  18.     // show taxonomy list
  19.     function show_field_taxonomy($field, $meta) {
  20.         global $post;
  21.        
  22.         if (!is_array($meta)) $meta = (array) $meta;
  23.        
  24.         $this->show_field_begin($field, $meta);
  25.        
  26.         $options = $field['options'];
  27.         $terms = get_terms($options['taxonomy'], $options['args']);
  28.        
  29.         // checkbox_list
  30.         if ('checkbox_list' == $options['type']) {
  31.             foreach ($terms as $term) {
  32.                 echo "<input type='checkbox' name='{$field['id']}[]' value='$term->slug'" . checked(in_array($term->slug, $meta), true, false) . " /> $term->name<br/>";
  33.             }
  34.         }
  35.         // select
  36.         else {
  37.             echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
  38.        
  39.             foreach ($terms as $term) {
  40.                 echo "<option value='$term->slug'" . selected(in_array($term->slug, $meta), true, false) . ">$term->name</option>";
  41.             }
  42.             echo "</select>";
  43.         }
  44.        
  45.         $this->show_field_end($field, $meta);
  46.     }
  47.  
  48.     // show custom post list
  49.     function show_field_custom_post($field, $meta) {
  50.         global $post;
  51.        
  52.         if (!is_array($meta)) $meta = (array) $meta;
  53.        
  54.         $this->show_field_begin($field, $meta);
  55.        
  56.  
  57.  
  58.         $options = $field['options'];
  59.  
  60.         $customs = get_posts($options['args']);
  61.        
  62.         // checkbox_list
  63.         if ('checkbox_list' == $options['type']) {
  64.             foreach ($customs as $custom) {
  65.                 $galleryurl = wp_get_attachment_image_src( get_post_thumbnail_id( $custom->ID ), 'full');
  66.                 echo "<input type='checkbox' name='{$field['id']}[]' value='$custom->post_title'" . checked(in_array($custom->post_title, $meta), true, false) . " />$custom->post_title<br/>";
  67.             }
  68.         }
  69.         // select
  70.         else {
  71.             echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
  72.        
  73.             foreach ($customs as $custom) {
  74.                 echo "<option value='$custom->post_title'" . selected(in_array($custom->post_title, $meta), true, false) . ">$custom->post_title</option>";
  75.             }
  76.             echo "</select>";
  77.         }
  78.        
  79.         $this->show_field_end($field, $meta);
  80.     }
  81. }
  82.  
  83.  
  84. // Show post_type meta box
  85. array(
  86.     'id' => $prefix . 'custom',
  87.     'type' => 'custom_post',                   
  88.     'options' => array(
  89.         'type' => 'checkbox_list',
  90.         'args' => array(          // arguments for get posts http://goo.gl/OVtT
  91.             'post_type' => 'artist',
  92.             'posts_per_page' => 3
  93.                 )
  94.             ),
  95.             'multiple' => true
  96.         )
  97.  
  98.  
  99.  
  100. // Renamed the class here with the new one
  101.  
  102. foreach ($meta_boxes as $meta_box) {
  103.     $my_box = new RW_Meta_Box_Extend($meta_box);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement