[英]rewrite_rules wordpress URL
我正在嘗試重寫 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 可能是錯誤的,但曲棍球oefening 頁面的 id:563 和注冊頁面 id:11。
試試這個下面的代碼
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' );
希望它有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.