Skip to content

Instantly share code, notes, and snippets.

@vovadocent
Last active December 16, 2016 10:48
Show Gist options
  • Save vovadocent/30103fa8234ca2e1bc7639be9c9d43d4 to your computer and use it in GitHub Desktop.
Save vovadocent/30103fa8234ca2e1bc7639be9c9d43d4 to your computer and use it in GitHub Desktop.
CPT post permalink with taxonomy slug
<?php
/*
'rewrite' => array('slug' => 'scripts/%custom-taxonomy%'), // register post type arg 'rewrite'
*/
add_filter('post_type_link', 'change_cpt_post_permalink', 99, 3);
function change_cpt_post_permalink($permalink, $post_id) {
if (strpos($permalink, '%custom-taxonomy%') === FALSE)
return $permalink;
$post = get_post($post_id);
if (!$post)
return $permalink;
$terms = wp_get_object_terms($post->ID, 'scripts-cat');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else
$taxonomy_slug = 'no-category';
return str_replace('%custom-taxonomy%', $taxonomy_slug, $permalink);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment