3

My Custom Structure is set to "/blog/%postname%/"

This works as expected, but for Posts with the Category "testimonials", I would like to change the URL structure to be "/testimonials/%postname%/".

I put the following code in my functions.php:

//Rewrite URLs for "testimonial" category
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the category for the post
    $category = get_the_category($post->ID);
    if (  !empty($category) && $category[0]->cat_name == "Testimonials" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->post_name .'/' ) );
    }
    return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    $regex = '/[\s\S]/';
    $new_rules = array(
      trailingslashit('testimonials/'.$regex) => '/?p='. get_page_by_path('$matches[1]', OBJECT, 'post')->id
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

This generates the desired URL, "myurl.com/testimonials/post-name" when I try to click on the desired Post, but then it returns a 404 error.

I'd like to accomplish the URL rewrite without needing to register a new Custom Post Type.

5

Try these steps:

Step #1: Replace this:

add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    $regex = '/[\s\S]/';
    $new_rules = array(
      trailingslashit('testimonials/'.$regex) => '/?p='. get_page_by_path('$matches[1]', OBJECT, 'post')->id
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

..with this one:

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        'testimonials/([^/]+)(?:/([0-9]+))?/?$',
        'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );
}

Step #2: Go to the Permalink Settings page, and click on the Save Changes button without actually making any changes.

| improve this answer | |
  • 1
    This was perfect, thanks. Can you point me toward any resources or tips on how to do the same for the Category page? Currently the category page shows as /blog/category/testimonials/, and I would like to change it to just /testimonials/ I tried the plugin here wordpress.org/plugins/custom-permalinks, which works, but then breaks your above solution for individual posts and returns a 404 – user3183717 Mar 13 '18 at 18:41
  • 1
    You're welcome! And as for the Category page, take a look at my answer, which I wrote in a similar style/format to the one for this question. – Sally CJ Mar 13 '18 at 23:09
  • This works great, but is there a way to not affect subfolder pages with the same category name? My use case is to create a subfolder section of the website, so all posts with the category "learn" have website.com/learn. I also have pages that us the same subfolder name, website.com/learn/page. The above code works perfectly for posts, but I get a 404 for pages. If I change the rule position to "bottom", the code works for pages, but then I get a 404 for posts. Is there a way to have the rewrite rule only affect posts? – agentcurry Mar 16 at 7:50
  • @agentcurry, I'd love to help, but I suggest you to post that as a new question. :) – Sally CJ Mar 16 at 13:19
  • 1
    Thanks, @SallyCJ, will do! – agentcurry Mar 17 at 7:41

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.