Last active
August 15, 2024 23:25
-
-
Save vovadocent/30103fa8234ca2e1bc7639be9c9d43d4 to your computer and use it in GitHub Desktop.
CPT post permalink with taxonomy slug
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 | |
/* | |
'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