what I want is if the category is "news" add /news-event/ before it.
Updated: its not working yet.
thanks to @jas I think this code will do it but I think I need to rewrite rule to make it works, Im not good at regex so I can not know how to do it, this is what I have tried so far.
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "news" ) {
$permalink = trailingslashit( home_url('news-events/' . $category. '/' . $post->post_name .'/' ) );
}
return $permalink;
}
function rewrite_rules( $wp_rewrite ) {
$new_rules['news-events/^news/([^/]+)/?'] = 'index.php?name=$matches[1]'; //change 'test' to your category slug
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_action('generate_rewrite_rules', 'rewrite_rules');
my site has the structure permalink like this "site.com/en/postname1", I have post category "category1, category2,..." I want to turn my post category into "site.com/en/category1/custom-text/postname1 or site.com/en/category2/custom-text/postname2". is it possible? Please help me.