0

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.

1

Please go to Settings > Permalinks. Have a look at the Permalinks page for other possibilities.

For You I think you need to use:

/%category%/%postname%

Hope this help!

UPDATE :

For additional slug based on category :

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/'. $post->post_name .'/' ) );
  }
    return $permalink;
}

Please adjust above code according to requirements for more details please follow this answer or this page

  • Hi, thanks, it works but now I need to add one more custom slug like if category1 -> /news/category1/postname1 ---- category2 -> /artical/category2/postname2, is it possible? – Yoona Oct 12 '16 at 15:41
  • I have added some code which will results your permalinks with additional slugs and please follow link to answer for detailed explanation ! Additional Link for similar solution and explanation, discussion wordpress.org/support/topic/… – jas Oct 12 '16 at 17:55
  • No Problem and happy to hear its working for you :) – jas Oct 12 '16 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.