• Hi, I want to do if the category is “news” add /news-event/ before it. so “site.com/news-event/news/post-name/”.
    This is what I have tried so far but it still not work.please help me if you see something might go wrong.

    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[2]'; //change 'test' to your category slug
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
        return $wp_rewrite;
    }
    add_action('generate_rewrite_rules', 'rewrite_rules'); 
    • This topic was modified 8 years, 4 months ago by nisoran123.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘custom permalinks for specific category’ is closed to new replies.