我正在尝试重写URL
来自: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可能是错的,但曲棍球优化页面的id: 563,注册页面id: 11。
发布于 2017-06-09 22:41:16
尝试下面的代码
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' );希望它能起作用
https://stackoverflow.com/questions/44454919
复制相似问题