Created
October 9, 2019 10:39
-
-
Save sungraizfaryad/d59f8b8177d6157c16c72dc572d54247 to your computer and use it in GitHub Desktop.
Remove Taxonomies name from URL.
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
| <?php | |
| function inspiry_remove_tax_slugs($query_vars){ | |
| // Add the slugs of those taxonomies which you want to remove from url. | |
| $tax_slugs = array('property-type','property-status','property-city'); | |
| if( isset($query_vars['attachment']) ? $query_vars['attachment'] : null) : | |
| $include_children = true; | |
| $name = $query_vars['attachment']; | |
| else: | |
| if( isset($query_vars['name']) ? $query_vars['name'] : null) { | |
| $include_children = false; | |
| $name = $query_vars['name']; | |
| } | |
| endif; | |
| if (isset($name)): | |
| foreach ($tax_slugs as $slug ) { | |
| $term = get_term_by('slug', $name, $slug); | |
| if ($term && !is_wp_error($term)): | |
| if( $include_children ) { | |
| unset($query_vars['attachment']); | |
| $parent = $term->parent; | |
| while( $parent ) { | |
| $parent_term = get_term( $parent, $slug); | |
| $name = $parent_term->slug . '/' . $name; | |
| $parent = $parent_term->parent; | |
| } | |
| } else { | |
| unset($query_vars['name']); | |
| } | |
| $query_vars[$slug] = $name; | |
| endif; | |
| } | |
| endif; | |
| return $query_vars; | |
| } | |
| add_filter('request', 'inspiry_remove_tax_slugs', 1, 1 ); | |
| function inspiry_build_tax_slugs( $url, $term, $taxonomy ){ | |
| // Add the slugs of those taxonomies which you want to remove from url. | |
| $taxonomy_slugs = array('property-type','property-status','property-city'); | |
| foreach ($taxonomy_slugs as $taxonomy_slug) { | |
| if ( stripos($url, $taxonomy_slug) === TRUE || $taxonomy == $taxonomy_slug ) { | |
| $url = str_replace('/' . $taxonomy_slug, '', $url); | |
| } | |
| } | |
| return $url; | |
| } | |
| add_filter( 'term_link', 'inspiry_build_tax_slugs', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment