Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // In functions.php
- /**
- * Custom Post Type
- */
- class location_post_type {
- function location_post_type() {
- add_action('init',array($this,'create_post_type'));
- }
- function create_post_type() {
- $labels = array(
- 'name' => 'Locations',
- 'singular_name' => 'Location',
- 'add_new' => 'Add New',
- 'all_items' => 'All Locations',
- 'add_new_item' => 'Add New Location',
- 'edit_item' => 'Edit Location',
- 'new_item' => 'New Location',
- 'view_item' => 'View Location',
- 'search_items' => 'Search Locations',
- 'not_found' => 'No Locations found',
- 'not_found_in_trash' => 'No Locations found in trash',
- 'parent_item_colon' => 'Parent Location:',
- 'menu_name' => 'Locations'
- );
- $args = array(
- 'labels' => $labels,
- 'description' => "A description for your Location type",
- 'public' => true,
- 'exclude_from_search' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'show_in_nav_menus' => true,
- 'show_in_menu' => true,
- 'show_in_admin_bar' => true,
- 'menu_position' => 20,
- 'menu_icon' => '/absolute/url/to/icon',
- 'capability_type' => 'post',
- 'hierarchical' => true,
- 'supports' => array('title','editor','page-attributes'),
- 'has_archive' => true,
- 'rewrite' => array('slug' => 'your-slug', 'with_front' => 'before-your-slug'),
- 'query_var' => true,
- 'can_export' => true
- );
- register_post_type('location_post_type',$args);
- }
- }
- $location_post_type = new location_post_type();
- /**
- * Add Parent dropdown to 'pages' that lists all location CPTs
- */
- add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'location_post_type', 'normal');});
- add_action('add_meta_boxes', function() { add_meta_box('page-parent', 'Parent', 'my_page_attributes_meta_box', 'page', 'side', 'high');});
- function my_page_attributes_meta_box($post) {
- $post_type_object = get_post_type_object($post->post_type);
- if ( $post_type_object->hierarchical ) {
- $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));
- if ( ! empty($pages) ) {
- echo $pages;
- } // end empty pages check
- } // end hierarchical check.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement