Advertisement
Guest User

CPT parent example

a guest
Apr 15th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. // In functions.php
  2.  
  3. /**
  4.  * Custom Post Type
  5.  */
  6. class location_post_type {
  7.    
  8.     function location_post_type() {
  9.         add_action('init',array($this,'create_post_type'));
  10.     }
  11.    
  12.     function create_post_type() {
  13.         $labels = array(
  14.             'name' => 'Locations',
  15.             'singular_name' => 'Location',
  16.             'add_new' => 'Add New',
  17.             'all_items' => 'All Locations',
  18.             'add_new_item' => 'Add New Location',
  19.             'edit_item' => 'Edit Location',
  20.             'new_item' => 'New Location',
  21.             'view_item' => 'View Location',
  22.             'search_items' => 'Search Locations',
  23.             'not_found' =>  'No Locations found',
  24.             'not_found_in_trash' => 'No Locations found in trash',
  25.             'parent_item_colon' => 'Parent Location:',
  26.             'menu_name' => 'Locations'
  27.         );
  28.         $args = array(
  29.             'labels' => $labels,
  30.             'description' => "A description for your Location type",
  31.             'public' => true,
  32.             'exclude_from_search' => true,
  33.             'publicly_queryable' => true,
  34.             'show_ui' => true,
  35.             'show_in_nav_menus' => true,
  36.             'show_in_menu' => true,
  37.             'show_in_admin_bar' => true,
  38.             'menu_position' => 20,
  39.             'menu_icon' => '/absolute/url/to/icon',
  40.             'capability_type' => 'post',
  41.             'hierarchical' => true,
  42.             'supports' => array('title','editor','page-attributes'),
  43.             'has_archive' => true,
  44.             'rewrite' => array('slug' => 'your-slug', 'with_front' => 'before-your-slug'),
  45.             'query_var' => true,
  46.             'can_export' => true
  47.         );
  48.         register_post_type('location_post_type',$args);
  49.     }
  50. }
  51.  
  52. $location_post_type = new location_post_type();
  53.  
  54.  
  55. /**
  56.  * Add Parent dropdown to 'pages' that lists all location CPTs
  57.  */
  58. add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'location_post_type', 'normal');});
  59. add_action('add_meta_boxes', function() { add_meta_box('page-parent', 'Parent', 'my_page_attributes_meta_box', 'page', 'side', 'high');});
  60.  
  61. function my_page_attributes_meta_box($post) {
  62.     $post_type_object = get_post_type_object($post->post_type);
  63.     if ( $post_type_object->hierarchical ) {
  64.       $pages = wp_dropdown_pages(array('post_type' => 'location_post_type', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
  65.       if ( ! empty($pages) ) {
  66.         echo $pages;
  67.       } // end empty pages check
  68.     } // end hierarchical check.
  69.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement