Advertisement
Guest User

custom post type projects with custom taxonomy

a guest
Mar 29th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.70 KB | None | 0 0
  1. <?php
  2. /*
  3. //
  4. // Custom Post Type: Projects
  5. //
  6. */
  7.  
  8.  
  9. /* ------ [ Project Post Type ] ------ */
  10. register_post_type(
  11.     'project',
  12.     array(
  13.         'label' => 'Projects',
  14.         'description' => 'Project Portfolio',
  15.         'public' => true,
  16.         'show_ui' => true,
  17.         'show_in_menu' => true,
  18.         'capability_type' => 'post',
  19.         'hierarchical' => false,
  20.         'rewrite' => false,
  21.         'menu_position' => 5,
  22.         'publicly_queryable' => true,
  23.         'query_var' => true,
  24.         'supports' => array(
  25.             'title',
  26.             'editor'
  27.         ),
  28.         'labels' => array (
  29.             'name' => 'Project',
  30.             'singular_name' => 'Project',
  31.             'menu_name' => 'Project List',
  32.             'add_new' => 'Add Project',
  33.             'add_new_item' => 'Add Project',
  34.             'edit' => 'Edit Projects',
  35.             'edit_item' => 'Edit Project',
  36.             'new_item' => 'New Project',
  37.             'view' => 'View Projects',
  38.             'view_item' => 'View Project',
  39.             'search_items' => 'Search Projects',
  40.             'not_found' => 'No Projects Found',
  41.             'not_found_in_trash' => 'No Projects in Trash',
  42.             'parent' => 'Parent Project'
  43.         )
  44.     )
  45. );
  46. global $wp_rewrite;
  47. $project_structure = '/portfolio/%project%';
  48. $wp_rewrite->add_rewrite_tag("%project%", '([^/]+)', "project=");
  49. $wp_rewrite->add_permastruct('project', $project_structure, false);
  50.  
  51.  
  52. /* ------ [ Project Metaboxes ] ------ */
  53. add_filter( 'cmb_meta_boxes', 'cmb_my_metaboxes' );
  54. function cmb_my_metaboxes(array $meta_boxes){
  55.  
  56. $prefix = 'project_';
  57.  
  58. $meta_boxes[] = array(
  59.     'id' => 'project_details',
  60.     'title' => 'Project Details',
  61.     'pages' => array('project',), // post type
  62.     'context' => 'normal',
  63.     'priority' => 'high',
  64.     'show_names' => true, // Show field names on the left
  65.     'fields' => array(
  66.         array(
  67.             'name' => 'Client',
  68.             'desc' => 'Client(s) name',
  69.             'id' => $prefix . 'client',
  70.             'type' => 'text_medium',
  71.         ),
  72.     array(
  73.             'name' => 'Client url',
  74.             'desc' => 'Website for the client',
  75.             'id' => $prefix . 'client_url',
  76.             'type' => 'text_medium',
  77.     ),
  78.     array(
  79.             'name' => 'Architect',
  80.             'desc' => 'Architect or Secondary Client',
  81.             'id' => $prefix . 'architect',
  82.             'type' => 'text_medium',
  83.     ),
  84.     array(
  85.             'name' => 'Architect url',
  86.             'desc' => 'Website for the architect',
  87.             'id' => $prefix . 'architect_url',
  88.             'type' => 'text_medium',
  89.     ),
  90.     array(
  91.         'name' => 'Project Location',
  92.         'desc' => 'City, State',
  93.         'id' => $prefix . 'location',
  94.         'type' => 'text_medium',
  95.     ),
  96.         array(
  97.             'name' => 'Featured Photo',
  98.             'id' => $prefix . 'featured_image',
  99.             'desc' => 'Attach image file (jpg, png, gif)',
  100.             'type' => 'file',
  101.         'allow' => array( 'url', 'attachment' ),
  102.         ),
  103.     array(
  104.             'name' => 'Date',
  105.             'desc' => 'Project completion date',
  106.             'id' => $prefix . 'date',
  107.             'type' => 'text_date',
  108.     ),
  109.         array(
  110.         'name' => 'Additional Project Photos',
  111.         'desc' => 'List of photos for project gallery',
  112.         'id' => $prefix . 'photo_list',
  113.         'type' => 'file_list',
  114.     ),
  115.         array(
  116.             'name' => 'Short Description',
  117.             'desc' => 'Display on portfolio page, limit to 200 words',
  118.             'id' => $prefix . 'short_description',
  119.             'type' => 'textarea_small',
  120.         ),
  121.     )
  122. );
  123.  
  124. /* ------ [ Project Long Bio ] ------ */
  125. $meta_boxes[] = array(
  126.     'id' => 'project_description',
  127.     'title' => 'Project Description',
  128.     'pages' => array('project'), // post type
  129.     'context' => 'normal',
  130.     'priority' => 'high',
  131.     'show_names' => true, // Show field names left of input
  132.         'fields' => array( )
  133. );
  134.  
  135. /* ------ [ Associated Project Type to Page ] ------ */
  136.  
  137. $meta_boxes[] = array(
  138.     'id' => 'page_project_type',
  139.     'title' => 'Attached Project Gallery',
  140.     'pages' => array('page'), // post type
  141.     'context' => 'normal',
  142.     'priority' => 'high',
  143.     'show_names' => true, // Show field names left of input
  144.         'fields' => array(
  145.         array(
  146.             'name' => 'Associated Project Type',
  147.             'desc' => 'Project Type gallery that will display on this page',
  148.             'id' => 'page_assoc_project_type',
  149.             'taxonomy' => 'project_type', //Enter Taxonomy Slug
  150.             'type' => 'taxonomy_multicheck',
  151.         ),
  152.         )
  153. );
  154.  
  155.     return $meta_boxes;
  156. }
  157.  
  158.  
  159. /* ------ [ Move Post Editor Into Long Description for Project Post Type ] ------ */
  160. function project_move_posteditor( $hook ) {
  161.     if ( $hook == 'post.php' OR $hook == 'post-new.php' ) {
  162.         wp_enqueue_script( 'jquery' );
  163.         add_action('admin_print_footer_scripts','project_move_posteditor_scripts');
  164.     }
  165. }
  166. add_action( 'admin_enqueue_scripts', 'project_move_posteditor', 10, 1 );
  167.  
  168. function project_move_posteditor_scripts() {
  169.     ?>
  170.     <script type="text/javascript">
  171.         jQuery('#postdiv, #postdivrich').prependTo('#project_description .inside');
  172.     </script>
  173.     <style type="text/css">
  174.             #normal-sortables {margin-top: 20px;}
  175.             #titlediv { margin-bottom: 0px; }
  176.             #postdiv.postarea, #postdivrich.postarea { margin:0; }
  177.             #post-status-info { line-height:1.4em; font-size:13px; }
  178.             #custom_editor .inside { margin:2px 6px 6px 6px; }
  179.             #ed_toolbar { display:none; }
  180.             #postdiv #ed_toolbar, #postdivrich #ed_toolbar { display:block; }
  181.     </style>
  182.     <?php
  183. }
  184.  
  185.  
  186.  
  187. /* ------ [ Rename Post Box Title Text ] ------ */
  188. add_action( 'gettext', 'project_change_title_text' );
  189. function project_change_title_text( $translation ) {
  190.     global $post;
  191.     if( isset( $post ) ) {
  192.         switch( $post->post_type ){
  193.             case 'project' :
  194.                 if( $translation == 'Enter title here' ) return 'Enter Project Name Here';
  195.             break;
  196.         }
  197.     }
  198.     return $translation;
  199. }
  200.  
  201.  
  202. /* ------ [ Adding Custom Columns in Project Admin Screen ] ------ */
  203. function add_new_project_columns($project_columns) {
  204.     $new_columns['cb'] = '<input type="checkbox" />';
  205.  
  206.     $new_columns['title'] = _x('Project Name', 'column name');
  207.     $new_columns['desc'] = __('Short Description');
  208.     $new_columns['type'] = __('Project Types');
  209.  
  210.     return $new_columns;
  211. }
  212.  
  213. function manage_project_columns($column_name, $id) {
  214.     global $typenow;
  215.     global $post;
  216.    
  217.     if ($typenow=='project') {
  218.     $taxonomy = 'project_type';
  219.        
  220.         switch ($column_name) {
  221.         case 'type':
  222.     $types = get_the_terms( $post->ID ,$taxonomy);
  223.     if (is_array($types )) {
  224.                 foreach($types as $key => $type ) {
  225.                     $edit_link = get_term_link( $type, $taxonomy );
  226.                     $term = get_term( $type, $taxonomy );
  227.             $slug = $term->slug;
  228.                     $types[$key] = '<a href="edit.php?'.$taxonomy.'='.$slug.'&post_type='.$typenow.'">' . $type->name . '</a>';
  229.                 }
  230.                 //echo implode("<br/>",$businesses);
  231.                 echo implode(' | ',$types);
  232.             }
  233.             break;
  234.    
  235.     case 'desc':
  236.         echo get_post_meta( $post->ID, 'project_short_description', TRUE);
  237.     break;
  238.    
  239.     default:
  240.         break;
  241.     } // end switch
  242. }
  243. }
  244.  
  245. add_filter('manage_edit-project_columns', 'add_new_project_columns');
  246. add_action('manage_project_posts_custom_column', 'manage_project_columns', 10, 2);
  247.  
  248.  
  249. /* ------ [ Project Type Taxonomy ] ------ */
  250. function dcg_register_project_type_taxonomy() {
  251.     $labels = array(
  252.         'name' => 'Project Type',
  253.         'singular_name' => 'Project Type',
  254.         'search_items' =>  'Search Project Types',
  255.         'all_items' => 'All Project Types',
  256.         'edit_item' => 'Edit Project Type',
  257.         'update_item' => 'Update Project Type',
  258.         'add_new_item' => 'Add New Project Type',
  259.         'new_item_name' => 'New Project Type Name',
  260.         'menu_name' => 'Project Types'
  261.     );  
  262.  
  263.     register_taxonomy( 'project_type', array('project'),
  264.         array(
  265.             'hierarchical' => true,
  266.             'labels' => $labels,
  267.             'show_ui' => true,
  268.             'query_var' => true,
  269.             'rewrite' => array( 'slug' => 'project_type' ),
  270.         )
  271.     );
  272. }
  273. add_action( 'init', 'dcg_register_project_type_taxonomy',1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement