I’ve been banging my head against a wall on this one for a while but I’m not quite sure from all the tutorials I read how to see why this isn’t doing what I expect.
I have a page called shop-guide/shop-page?id=412
(for example)
and I want to change it so it it’s shop-guide/shop-page/id/412/
As far as I can tell I have my code set up correctly, but when I try to navigate to the page I get a 404, and if I use the $_GET version it doesn’t rewrite.
Here’s my code: (in functions.php
)
function wptuts_add_rewrite_rules() {
add_rewrite_rule(
'shop-guide/shop-page/id/([^/]*)/',
'index.php?p=143&id=$matches[1]',
'top'
);
}
add_action( 'init', 'wptuts_add_rewrite_rules' );
Two issues –
your rewrite rule isn’t formatted correctly, this:
should be:
and if
shop-guide
andshop-page
are parent / child pages, this:should be:
I believe you first need to register your custom querystring variable. You could use
add_rewrite_tag
for this purpose: http://codex.wordpress.org/Rewrite_API/add_rewrite_ruleSee the important note down at the bottom of the codex article
Edit:
You could also use
query_vars
hook to add your own custom query variables.