rewrite_rules wordpress URL

时间:2017-06-09 10:03:37

标签: php wordpress .htaccess word

您好我的网站我试图从

重写网址
  

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

  

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

我尝试手动添加.htaccess文件。但是我没有让它发挥作用。所以我重置了.htaccess文件,并寻找另一种方式。

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

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数组添加了一条规则,我可以看到

global $wp_rewrite;
print_r($wp_rewrite);

输出:

   [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可能有误,但是曲棍球oefening页面的id为:563,注册页面为id:11。

我无法弄清楚我做错了什么。你们有什么想法吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

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' );

希望它有效