Last active
August 4, 2017 21:09
-
-
Save thefuxia/6034374 to your computer and use it in GitHub Desktop.
CPT with tax slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php # -*- coding: utf-8 -*- | |
namespace CPTPermalinkDemo; | |
/** | |
* Plugin Name: Custom post type with taxonomy permalink | |
* Description: | |
* Plugin URI: | |
* Version: 2013.07.19 | |
* Author: Thomas Scholz | |
* Author URI: http://toscho.de | |
* Licence: MIT | |
* License URI: http://opensource.org/licenses/MIT | |
* Textdomain: | |
* Domain Path: /languages | |
* Network: | |
*/ | |
\add_action( 'init', array ( __NAMESPACE__ . '\Controller', 'init' ) ); | |
class Controller { | |
protected $post_type = 'product'; | |
protected $taxonomy = 'color'; | |
public static function init() | |
{ | |
static $instance = NULL; | |
if ( NULL === $instance ) | |
$instance = new self; | |
return $instance; | |
} | |
protected function __construct() | |
{ | |
$permalink = new Permalink( $this->post_type, $this->taxonomy ); | |
\add_filter( 'post_type_link', array ( $permalink, 'filter_permalink' ), 10, 2 ); | |
$post_type = new Post_Type( $this->post_type, $this->taxonomy ); | |
$taxonomy = new Taxonomy( $this->taxonomy, $this->post_type ); | |
} | |
} | |
class Post_Type | |
{ | |
protected $taxonomy; | |
public function __construct( $post_type, $taxonomy ) | |
{ | |
$this->taxonomy = $taxonomy; | |
\register_post_type( $post_type, $this->get_args( $post_type ) ); | |
} | |
protected function get_args( $post_type ) | |
{ | |
$u = \ucfirst( $post_type ); | |
$l = \mb_strtolower( $post_type ); | |
return array ( | |
'can_export' => TRUE, | |
'description' => '', | |
'exclude_from_search' => FALSE, | |
'has_archive' => TRUE, | |
'hierarchical' => FALSE, | |
'labels' => array ( | |
'add_new' => 'Add new', | |
'add_new_item' => "Add new $l", | |
'all_items' => "All {$l}s", | |
'edit_item' => "Edit $l", | |
'name' => $u . 's', | |
'name_admin_bar' => $u, | |
'new_item' => "New $l", | |
'not_found' => "No {$l}s found.", | |
'not_found_in_trash' => "No {$l}s in trash.", | |
'parent_item_colon' => "Parent $l:", | |
'search_items' => "Search {$l}s", | |
'singular_name' => $u, | |
'view_item' => "View $l" | |
), | |
//'map_meta_cap' => TRUE, | |
'menu_icon' => null, | |
'menu_position' => null, | |
'permalink_epmask' => EP_PERMALINK, | |
'public' => TRUE, | |
'publicly_queryable' => TRUE, | |
'query_var' => $l, | |
// Use a translated slug in pretty permalinks | |
'rewrite' => array ( | |
//'slug' => $l, | |
'slug' => "%$this->taxonomy%", | |
'with_front' => FALSE | |
), | |
'show_in_nav_menus' => TRUE, | |
'show_in_menu' => TRUE, | |
'show_in_admin_bar' => TRUE, | |
'show_ui' => TRUE, | |
'supports' => array( | |
'author', | |
'comments', | |
'editor', | |
'excerpt', | |
'revisions', | |
'thumbnail', | |
'title', | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, GitHub cannot save the rest of the file as a Gist, so I had to put it into a comment.