I am trying to:
Display URL like /cpt-name/category/sub-category/post-name
Instead, I got:
/cpt-name/category/post-name
I can't seem to be able to place the sub-category into the URL.
I created the CPT and under rewrite i set it to custom:
cpt-name/%my-cpt-taxonomy%
I added the code i saw in this forum:
// custom taxonomy permalinks
add_filter('post_link', 'taxonomy_permalink', 10, 3);
add_filter('post_type_link', 'taxonomy_permalink', 10, 3);
function taxonomy_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%my-cpt-taxonomy%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'gender');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = '';
return str_replace('%my-cpt-taxonomy%', $taxonomy_slug, $permalink);
}
It works... but only for the main category and not sub-categories.
Wordpress permalinks is set to /%category%/%postname%/
I've checked this posts and still no go:
https://toolset.com/forums/topic/custom-taxonomy-permalinks/
https://toolset.com/forums/topic/taxonomy-permalink-structure/
https://toolset.com/forums/topic/adding-custom-taxonomy-to-url-and-permalink-not-working/#post-16948
Could anyone tell me what i'm doing wrong?
thank you
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - this is custom code and even if you search the whole forum you might end finding no solution. As per our support policy we do not support custom edits. This is beyond the scope of our support policy.
I suggest you should use the plugin such as that will help you to format your permalink as per your need:
=> https://wordpress.org/plugins/wp-better-permalinks/