Last active
January 29, 2018 10:18
-
-
Save alimoshen/15159c0f0d83163d1935b28b5053e61a to your computer and use it in GitHub Desktop.
WordPress taxonomy trail in slug
This file contains hidden or 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
// Post type 'expertise' | |
add_action( 'init', 'create_expertise_post_type', 4 ); | |
function create_expertise_post_type() | |
{ | |
$labels = array( | |
'name' => __( 'Expertise' ), | |
'singular_name' => __( 'Expertise' ), | |
'add_new' => __( 'Add New' ), | |
'add_new_item' => __( 'Create New' ), | |
'edit' => __( 'Edit' ), | |
'edit_item' => __( 'Edit' ), | |
'new_item' => __( 'New' ), | |
'view' => __( 'View' ), | |
'view_item' => __( 'View' ), | |
'search_items' => __( 'Search' ), | |
'not_found' => __( 'Not found' ), | |
'not_found_in_trash' => __( 'Nothing found in trash' ), | |
'parent' => __( 'Parent' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => __( 'This is where you can create a new Item.' ), | |
'public' => true, | |
'show_ui' => true, | |
'capability_type' => 'post', | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
'menu_position' => 4, | |
'hierarchical' => true, | |
'_builtin' => false, // It's a custom post type, not built in! | |
'rewrite' => array( 'slug' => 'expertise/%expertise_type%', 'with_front' => true ), | |
'query_var' => true, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ), | |
'taxonomies' => array('expertise_type'), | |
); | |
register_post_type('expertise',$args); | |
} |
This file contains hidden or 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
add_action( 'init', 'create_expertise_type_taxonomies', 0 ); | |
function create_expertise_type_taxonomies() | |
{ | |
// Add new taxonomy, make it hierarchical => true for categories-like taxonomies) | |
$labels = array( | |
'name' => _x( 'Expertise', 'taxonomy general name' ), | |
'singular_name' => _x( 'Expertise', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Expertise' ), | |
'all_items' => __( 'All Expertises' ), | |
'parent_item' => __( 'Parent Expertise' ), | |
'parent_item_colon' => __( 'Parent Expertise:' ), | |
'edit_item' => __( 'Edit Expertise' ), | |
'update_item' => __( 'Update Expertise' ), | |
'add_new_item' => __( 'Add New Expertise' ), | |
'new_item_name' => __( 'New Expertise' ), | |
'menu_name' => __( 'Expertise' ), | |
); | |
register_taxonomy('expertise_type',array('expertise'), array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => array('hierarchical' => true), | |
)); | |
} |
This file contains hidden or 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
add_filter('rewrite_rules_array', 'mmp_rewrite_rules'); | |
function mmp_rewrite_rules($rules) { | |
$newRules = array(); | |
$newRules['expertise/(.+)/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?expertise=$matches[5]'; // use the 5th tax segment | |
$newRules['expertise/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?expertise=$matches[4]'; // use the 4th tax segment | |
$newRules['expertise/(.+)/(.+)/(.+)/?$'] = 'index.php?expertise=$matches[3]'; // use the 3rd tax segment | |
$newRules['expertise/(.+)/(.+)/?$'] = 'index.php?expertise=$matches[2]'; // use the 2nd tax segment | |
$newRules['expertise/(.+)/(.+)/?$'] = 'index.php?expertise_type=$matches[2]'; | |
$newRules['expertise/(.+)/?$'] = 'index.php?expertise_type=$matches[1]'; | |
return array_merge($newRules, $rules); | |
} | |
function filter_post_type_link($link, $post) | |
{ | |
if ($post->post_type != 'expertise') | |
return $link; | |
if ($cats = get_the_terms($post->ID, 'expertise_type')) | |
{ | |
$link = str_replace('%expertise_type%', get_taxonomy_parents(array_pop($cats)->term_id, 'expertise_type', false, '/', true), $link); // see custom function defined below | |
} | |
return $link; | |
} | |
add_filter('post_type_link', 'filter_post_type_link', 10, 2); | |
// my own function to do what get_category_parents does for other taxonomies | |
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) { | |
$chain = ''; | |
$parent = &get_term($id, $taxonomy); | |
if (is_wp_error($parent)) { | |
return $parent; | |
} | |
if ($nicename) | |
$name = $parent -> slug; | |
else | |
$name = $parent -> name; | |
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) { | |
$visited[] = $parent -> parent; | |
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited); | |
} | |
if ($link) { | |
// nothing, can't get this working :( | |
} else | |
$chain .= $name . $separator; | |
return $chain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment