Skip Navigation

[Resolved] Permalink structure with category but not sub-categories

This support ticket is created 5 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1240882

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

#1240941

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/