WordPress and add_rewrite_rule(): Assistance needed
-
On a WP website, one page grabs a slug from $_GET in order to display external news on the page. My base urls look like this
https://exemple.com/news-and-events/news/?id=assessing/litecoins/bounce-social-supremacy-and-everything-in-between/
I want pretty urls, like this:
https://exemple.com/news-and-events/news/assessing/litecoins/bounce-social-supremacy-and-everything-in-between/
I added the following in functions.php:
function my_rewrite() {
add_rewrite_tag(
'%id%',
'([^/]+)'
);
add_rewrite_rule(
'^news/([^/]+)/?',
'index.php?pagename=news&id=$matches[1]',
'top'
);
}
add_action( 'init', 'my_rewrite' );
in page.php:$id = get_query_var( 'id' );
// Grab data from the endpoint using $id
The redirect works, but if the slug after /news/ in my url contains a slash, get_query_var( ‘id’ ) returns only the characters within the first and second slash.I assume the issue is in my regexp but I can’t seem to make it work. Any ideas would be greatly appreciated.
- The topic ‘WordPress and add_rewrite_rule(): Assistance needed’ is closed to new replies.