首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rewrite_rules wordpress URL

rewrite_rules wordpress URL
EN

Stack Overflow用户
提问于 2017-06-09 18:03:37
回答 1查看 635关注 0票数 0

我正在尝试重写URL

来自:http://localhost/hockey-oefening/70/interceptie-warming-up

收件人:http://localhost/hockey-oefening/?oefening=70&naam=interceptie-warming-up

我尝试手动添加.htaccess文件。然而,我没有让它工作。所以我重置了.htaccess文件,并寻找另一种方法。

然后,我在functions.php中添加了以下代码

代码语言:javascript
复制
function custom_rewrite_rule() {
    add_rewrite_tag('%oefening%', '([^&]+)');
    add_rewrite_tag('%naam%', '([^&]+)');
    add_rewrite_rule('^hockey-oefening/([^/]*)/([^/]*)?', 'index.php?page_id=563&oefening=$matches[1]&naam=$matches[2]', 'top' );
}
add_action('init', 'custom_rewrite_rule', 10, 2);

这将向$wp_rewrite数组中添加一条规则,如我所见

代码语言:javascript
复制
global $wp_rewrite;
print_r($wp_rewrite);

输出:

代码语言:javascript
复制
   [extra_rules_top] => Array
    (
        [^wp-json/?$] => index.php?rest_route=/
        [^wp-json/(.*)?] => index.php?rest_route=/$matches[1]
        [^index.php/wp-json/?$] => index.php?rest_route=/
        [^index.php/wp-json/(.*)?] => index.php?rest_route=/$matches[1]
        [service/?$] => index.php?post_type=services
        [service/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?post_type=services&feed=$matches[1]
        [service/(feed|rdf|rss|rss2|atom)/?$] => index.php?post_type=services&feed=$matches[1]
        [service/page/([0-9]{1,})/?$] => index.php?post_type=services&paged=$matches[1]
        [^hockey-oefening/([^/]*)/([^/]*)?] => index.php?page_id=563&oefening=$matches[1]&naam=$matches[2]
    )

现在,当我尝试访问http://localhost/hockey-oefening/70/interceptie-warming-up时,会得到一个到http://localhost/registreren/的重定向消息

现在我在想,我的page_id可能是错的,但曲棍球优化页面的id: 563,注册页面id: 11。

EN

回答 1

Stack Overflow用户

发布于 2017-06-09 22:41:16

尝试下面的代码

代码语言:javascript
复制
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
  $qvars[] = 'oefening';
  $qvars[] = 'naam';
  return $qvars;
}


//rewrite part
function wpse120653_init() {    
    add_rewrite_rule(
        'hockey-oefening(/([^/]+))?(/([^/]+))?/?',
        'index.php?pagename=hockey-oefening&oefening=$matches[2]&naam=$matches[4]',
        'top'       
    );
}   
add_action( 'init', 'wpse120653_init' );

希望它能起作用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44454919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档