custom permalinks for category archive ( custom base and no parent slug )
Question
For the default taxonomy i.e category , I want custom permalinks with
i) base = "post/c" and
ii) without any parent slug
For eg: if I have categories
cat-a
subcat-a1
subcat-a2
cat-b
subcat-a2
subcat-a2
so I want permalinks like
example.com/post/c/cat-a
example.com/post/c/subcat-a1
example.com/post/c/subcat-a2
example.com/post/c/cat-b
example.com/post/c/subcat-b1
example.com/post/c/subcat-b2
I am using the following code .. I solves the issue (ii) but not issue (i) i.e base ‘post/c’
add_filter('term_link', 'ojasya_no_term_parents', 1000, 3);
function ojasya_no_term_parents($url, $term, $taxonomy) {
if($taxonomy == 'category'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
}
return $url;
}
// Add our custom post cat rewrite rules
add_filter('rewrite_rules_array', 'ojasya_no_category_parents_rewrite_rules');
function ojasya_no_category_parents_rewrite_rules($rules) {
$new_rules = array();
$terms = get_terms( array(
'taxonomy' => 'category',
'post_type' => 'post',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
$new_rules[$term_slug.'/?$'] = 'index.php?category='.$term_slug;
$new_rules[$term_slug.'/page/([0-9]{1,})/?$'] = 'index.php?category='.$term_slug.'&paged=$matches[1]';
$new_rules[$term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
}
Please help me out on this . Thanks in advance
0
categories, permalinks
3 years
2020-07-13T16:11:13-05:00
2020-07-13T16:11:13-05:00 0 Answers
55 views
0
Leave an answer