Advertisement
Guest User

Portfolio Item

a guest
Aug 24th, 2010
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. class Portfolio {
  4.     var $meta_fields = array('p-website','p-thumb','p-main');
  5.    
  6.     function Portfolio()
  7.     {
  8.         // Register custom post types
  9.         register_post_type('portfolio', array(
  10.             'label' => __('Portfolio'),
  11.             'singular_label' => __('Portfolio'),
  12.             'public' => true,
  13.             'show_ui' => true, // UI in admin panel
  14.             'capability_type' => 'post',
  15.             'hierarchical' => false,
  16.             'rewrite' => array("slug" => "portfolio"), // Permalinks
  17.             'query_var' => "portfolio", // This goes to the WP_Query schema
  18.             'supports' => array('title', 'editor' /*,'custom-fields'*/) // Let's use custom fields for debugging purposes only
  19.         ));
  20.        
  21.         // Register custom taxonomy
  22.         register_taxonomy("type", array("portfolio"), array("hierarchical" => false, "label" => "Types", "singular_label" => "Type", "rewrite" => true));
  23.  
  24.         // Admin interface init
  25.         add_action("admin_init", array(&$this, "admin_init"));
  26.        
  27.         // Insert post hook
  28.         add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2);
  29.     }
  30.    
  31.     // When a post is inserted or updated
  32.     function wp_insert_post($post_id, $post = null)
  33.     {
  34.         if ($post->post_type == "portfolio")
  35.         {
  36.             // Loop through the POST data
  37.             foreach ($this->meta_fields as $key)
  38.             {
  39.                 $value = @$_POST[$key];
  40.                 if (empty($value))
  41.                 {
  42.                     delete_post_meta($post_id, $key);
  43.                     continue;
  44.                 }
  45.  
  46.                 // If value is a string it should be unique
  47.                 if (!is_array($value))
  48.                 {
  49.                     // Update meta
  50.                     if (!update_post_meta($post_id, $key, $value))
  51.                     {
  52.                         // Or add the meta data
  53.                         add_post_meta($post_id, $key, $value);
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     // If passed along is an array, we should remove all previous data
  59.                     delete_post_meta($post_id, $key);
  60.                    
  61.                     // Loop through the array adding new values to the post meta as different entries with the same name
  62.                     foreach ($value as $entry)
  63.                         add_post_meta($post_id, $key, $entry);
  64.                 }
  65.             }
  66.         }
  67.     }
  68.    
  69.     function admin_init()
  70.     {
  71.         // Custom meta boxes for the edit podcast screen
  72.         add_meta_box('p-website','Options',array(&$this,'meta_options'),'portfolio','side','low');
  73.     }
  74.    
  75.     // Admin post meta contents
  76.     function meta_options(){
  77.         global $post;
  78.         $custom = get_post_custom($post->ID);
  79.         $length = $custom["p-website"][0];
  80.         $thumb = $custom["p-thumb"][0];
  81.         $main = $custom["p-main"][0];
  82. ?>
  83.         <label>Website:</label><br /><p><input name="p-website" value="<?php echo $website; ?>" /></p><br />
  84. <?php
  85.         function get_attachments($postID,$type,$theone){
  86.             $args = array(
  87.                 'post_type' => 'attachment',
  88.                 'numberposts' => null,
  89.                 'post_status' => null,
  90.                 'post_parent' => $postID
  91.             );
  92.             $attachments = get_posts($args);
  93.             if($attachments){
  94.                 foreach($attachments as $attachment){
  95.                     $image = wp_get_attachment_image_src($attachment->ID,'full');
  96.                     if($image == $theone){$select = ' checked="yes"';}else{$select = '';};
  97.                     echo '<p><input type="radio" name="p-'.$type.'" value="'.$image[0].'"'.$select.' /> <img src="'.$image[0].'" height="60" width="60" alt="" /></p>';
  98.                 }
  99.             }
  100.             else{
  101.                 echo '<p><em>Please upload images</em></p><br />';
  102.             }
  103.         }
  104.  
  105.         echo '<label>Thumnail</label><br />';
  106.         get_attachments($post->ID,'thumb',$thumb);
  107.  
  108.         echo '<br /><label>Main</label><br />';
  109.         get_attachments($post->ID,'main',$main);
  110.     }
  111. }
  112.  
  113. // Initiate the plugin
  114. add_action("init", "PortfolioInit");
  115. function PortfolioInit() { global $p30; $p30 = new Portfolio(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement