3

I have a WordPress theme with Custom Post Types. My URL Structure is /%category%/%postname% (this has to stay that way)

I also have a Custom Post Type "Cars" and a Category "Cars". When I enter domain.com/cars, WordPress will route to the archive.php template file (which is correct default behaviour).

What I want instead, is that URL pointing to the pretty URL of a page (which would have a custom template) that has the Permalink domain.com/cars (ugly URL would be domain.com/index.php?pagename=cars)

I tried this:

add_action( 'init', 'add_rewrite_rules' );  
function add_rewrite_rules() {   
    add_rewrite_rule(  
        'cars/([^/]+)/?$',  
        'index.php?pagename=$matches[1]',  
        'top');
}  

I also flushed Rewrite Rules, but it does not work (nothing happens). Any Ideas?

2
  • WordPress automatically creates a permalink for your custom post type. So domain.com/%category%/%postname%/ doesn't work? Or are you trying to create a new permalink to show all and specific "cars"? Commented Nov 12, 2014 at 10:54
  • domain.com/%category%/%postname%/ works perfectly fine and domain.com/%customposttype%/postname works fine. Problem: I want to route domain.com/cars to a page instead the archive. Commented Nov 12, 2014 at 11:55

1 Answer 1

1

I found this solution (for functions.php):

The first line matches URLs like cars/mustang, the second one points /cars to the page named cars.

add_rewrite_rule('^cars/([^/]*)/?','cars/$matches[1]','top');
add_rewrite_rule('^cars$','index.php?pagename=cars','top');

Don't forget to flush the rewrite rules in Settings->Permalinks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.