admin管理员组文章数量:1335400
I'm trying to update my link from this: /?eb_event_id=106098978902 To this: /[guest_name_here]/[event_id_here]
Following this discussion, I was able to come up with this code:
In my functions.php:
/*rewrite rule*/
function custom_rewrite_rule() {
add_rewrite_rule('^next-event/([^/]*)/([^/]*)/?','index.php?page_id=69&guest_name=$matches[1]&event_id=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
$aVars[] = "guest_name";
$aVars[] = "event_id";
return $aVars;
}
/*end rewrite rule*/
In my next-event page:
/*
Template Name: Single Event Template (updated)
*/
get_header();
global $wp_query;
$guest_name = $wp_query->query_vars['guest_name'];
$test_eb_event_id = $wp_query->query_vars['event_id'];
echo $guest_name;
echo $test_eb_event_id;
When I try to test the URL, the rewrite is not working. I also clicked the save button in the permalinks page to apply the changes.
So when I enter: .php?page_id=69&guest_name=test&event_id=123
I get /?guest_name=test&event_id=123
Any help is highly appreciated. Thank you very much!
I'm trying to update my link from this: https://dev.brunchwork/next-event/?eb_event_id=106098978902 To this: https://dev.brunchwork/next-event/[guest_name_here]/[event_id_here]
Following this discussion, I was able to come up with this code:
In my functions.php:
/*rewrite rule*/
function custom_rewrite_rule() {
add_rewrite_rule('^next-event/([^/]*)/([^/]*)/?','index.php?page_id=69&guest_name=$matches[1]&event_id=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
$aVars[] = "guest_name";
$aVars[] = "event_id";
return $aVars;
}
/*end rewrite rule*/
In my next-event page:
/*
Template Name: Single Event Template (updated)
*/
get_header();
global $wp_query;
$guest_name = $wp_query->query_vars['guest_name'];
$test_eb_event_id = $wp_query->query_vars['event_id'];
echo $guest_name;
echo $test_eb_event_id;
When I try to test the URL, the rewrite is not working. I also clicked the save button in the permalinks page to apply the changes.
So when I enter: https://dev.brunchwork/index.php?page_id=69&guest_name=test&event_id=123
I get https://dev.brunchwork/next-event/?guest_name=test&event_id=123
Any help is highly appreciated. Thank you very much!
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked May 30, 2020 at 3:24 elimariaaaelimariaaa 1212 bronze badges1 Answer
Reset to default 0There's nothing wrong with my code. Maybe it was not refreshed instantly so the changes did not propagate right away.
本文标签: url rewritingDynamic URL with rewrite rule not working
版权声明:本文标题:url rewriting - Dynamic URL with rewrite rule not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742388071a2465437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论