Last active
January 27, 2016 18:56
-
-
Save chillbits-legacy/1da3f27dcb5e5439c72a to your computer and use it in GitHub Desktop.
Multiple categories rewrite rules in WordPress. Example: /cat/child-cat/grandchild-cat/cpt-name/
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 | |
class Your_Plugin { | |
/** | |
* Post Type slug modification | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
function modifySlug($link, $post) | |
{ | |
if ($post->post_type != $this->hotel->getUniqueID()) | |
return $link; | |
if ($cats = get_the_terms($post->ID, 'destinations')) | |
$link = str_replace('%destinations%', $this->getTaxonomyParents(array_pop($cats)->term_id, 'taxonomy_name', false, '/', true), $link); | |
return $link; | |
} | |
/** | |
* Chaining the taxonomies hierarchy | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
function getTaxonomyPath($id, $taxonomy,$tree=array(),$index=999) | |
{ | |
$node = &get_term($id, $taxonomy); | |
if (is_wp_error($node)) { | |
return $node; | |
} | |
$tree[$index] = $node->slug; | |
if ($node->parent and ($node->parent != $node->term_id)) | |
return $this->getTaxonomyPath($node->parent, $taxonomy, $tree, --$index); | |
ksort($tree); | |
return implode("/", $tree); | |
} | |
/** | |
* Add hooks and filters | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
public function hooks() { | |
add_filter( 'post_type_link', array($this,'modifySlug'), 10, 2); | |
add_filter( 'rewrite_rules_array', array($this,'modifyRewriteRules'),10,2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment