Skip to content

Instantly share code, notes, and snippets.

@fridaynext
Created April 20, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fridaynext/a00495da056fee8ad6c81271e13b8840 to your computer and use it in GitHub Desktop.
Save fridaynext/a00495da056fee8ad6c81271e13b8840 to your computer and use it in GitHub Desktop.
<?php
// custom post type permalinks
add_filter('post_type_link', 'deb_permalink', 10, 4);
function deb_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxonomy_name%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'product_types');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
$taxonomy_slug = '';
if ( $terms[0]->parent ) {
$taxonomy_slug .= $terms[1]->slug;
$taxonomy_slug .= '/';
}
$taxonomy_slug .= $terms[0]->slug;
} else
$taxonomy_slug = 'other';
return str_replace('%taxonomy_name%', $taxonomy_slug, $permalink);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment