Advertisement
Guest User

CPT for rewrite rules

a guest
Jul 3rd, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Custom Post Type Test
  4. Plugin URI: http://zed1.com/
  5. Description: Testing Custom Post Types in WordPress 3.1
  6. Author: Mike Little
  7. Version: 0.1.0
  8. Author URI: http://zed1.com/
  9. License: GPLv2 or later
  10. */
  11.  
  12. new cpt(); // kick it all off
  13. register_activation_hook( __FILE__, array( 'cpt', 'activation' ) );
  14. register_deactivation_hook( __FILE__, array( 'cpt', 'deactivation' ) );
  15.  
  16. class cpt {
  17.     function __construct() {
  18.         add_action( 'init', array( 'cpt', 'init' ) );
  19.     } // end constructor
  20.  
  21.     static function activation() {
  22.         self::register_cpt();
  23.         self::flush_rules();
  24.     } // end activation
  25.  
  26.     static function deactivation() {
  27.         self::flush_rules();
  28.     } // end deactivation
  29.  
  30.     static function flush_rules() {
  31.         flush_rewrite_rules();
  32.     } // end deactivation
  33.  
  34.     static function init() {
  35.         self::register_cpt();
  36.     } // end init
  37.  
  38.     static function register_cpt() {
  39.         if ( !post_type_exists( 'cpt' ) ) {
  40.             $labels = array( 'name' => _x( 'CPTs', 'post type general name' ),
  41.                              'singular_name' => _x( 'CPT', 'post type singular name' ),
  42.                              'add_new' => _x( 'New CPT', 'add new singular name' ),
  43.                              'add_new_item' => __( 'Add New CPT' ),
  44.                              'edit_item' => __( 'Edit CPT' ),
  45.                              'new_item' => __( 'New CPT' ),
  46.                              'view_item' => __( 'View CPT' ),
  47.                              'search_items' => __( 'Search CPTs' ),
  48.                              'not_found' =>  __( 'No CPTs Found' ),
  49.                              'not_found_in_trash' => __( 'No CPTs found in Trash' ),
  50.                              'parent_item_colon' => '-:-'
  51.                            );
  52.             $args = array( 'labels' => $labels,
  53.                            'public' => true,
  54.                            'publicly_queryable' => true,
  55.                            'show_ui' => true,
  56.                            'query_var' => true,
  57.                            'rewrite' => array( 'slug' => 'cpt',
  58.                                                'with_front' => true,
  59.                                                'feeds' => false,
  60.                                                'feed' => false,
  61.                                                'pages' => true,
  62.                                                'ep_mask' => 0 //(EP_ROOT)
  63.                                               ),
  64.                            'has_archive' => 'cpts',
  65.                            'capability_type' => 'post',
  66.                            'hierarchical' => true,
  67.                            'show_in_nav_menus' => true,
  68.                            'menu_position' => 100, // last block
  69.                            'supports' => array( 'title',
  70.                                                 'editor',
  71.                                                 'excerpt',
  72.                                                 //'comments',
  73.                                                 'revisions',
  74.                                                 'page-attributes',
  75.                                                 'shortlink' )
  76.                          );
  77.             register_post_type( 'cpt', $args );
  78.         }
  79.     } // end register_cpt
  80.  
  81. } // end class cpt
  82.  
  83. /* put this in your side bar
  84. <?php list_cpts(); ?>
  85. */
  86.  
  87. function list_cpts( $args = array() ) {
  88.     global $post;
  89.     $default_args = array( 'post_type'=>'cpt', 'title_li'=> __('CPTs') );
  90.  
  91.     if ( is_single() && ( 'cpt' == $post->post_type ) ) {
  92.         if ( $parent_id = wp_get_post_parent_id( $post->ID ) ) {
  93.             $default_args['child_of'] = $parent_id; // get siblings
  94.         } else {
  95.             $default_args['child_of'] = $post->ID; // get children
  96.         }
  97.     }
  98.  
  99.     $args = array_merge( $default_args, $args );
  100.     return wp_list_pages( $args );
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement