简体   繁体   English

rewrite_rules wordpress URL

[英]rewrite_rules wordpress URL

I'm trying to rewrite an URL我正在尝试重写 URL

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

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

I tried to add manually .htaccess file.我尝试手动添加 .htaccess 文件。 However, I did not get it to work.但是,我没有让它工作。 So I reset the .htaccess file and looked for another way.所以我重置了 .htaccess 文件并寻找另一种方式。

Then I added the following code in my functions.php然后我在我的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);

This adds a rule to the $wp_rewrite array as I can see with正如我所看到的,这向 $wp_rewrite 数组添加了一条规则

global $wp_rewrite;
print_r($wp_rewrite);

The output:输出:

   [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]
    )

Now when I try to go to http://localhost/hockey-oefening/70/interceptie-warming-up , I get a redirection to http://localhost/registreren/现在,当我尝试访问http://localhost/hockey-oefening/70/interceptie-warming-up ,会重定向到http://localhost/registreren/

Now I was thinking that my page_id might be wrong, but the hockey-oefening page has id: 563 and the register page id: 11.现在我在想我的 page_id 可能是错误的,但曲棍球oefening 页面的 id:563 和注册页面 id:11。

Try this following code试试这个下面的代码

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

Hope it works希望它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM