我只需要重定向 test.com/buying/properties-for-sale/?property=-north-gorley-11409
在 wordpress 中 test.com/buying/properties-for-sale/-north-gorley-11409
我嘗試了以下重寫,但結果是 404 頁
add_rewrite_rule('/buying/properties-for-sale-salisbury-2/?([^/]*)', 'index.php?pagename=properties-for-sale-salisbury-2&property=$matches[1]', 'top');
uj5u.com熱心網友回復:
將重寫規則查詢添加為可選的示例,希望它有所幫助。
function custom_rewrite_rule() {
add_rewrite_rule('^nutrition/?([^/]*)/?','index.php?page_id=12&food=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
這里的主要內容是添加 ? 在您的正則運算式開始時。喜歡?([^/]*)
現在,您可以為將在其中使用查詢的可選查詢設定默認值。
$food = $wp_query->get( 'food' );
if( isset($food) && !empty($food) ) {
$nutrition = $food;
}else{
$nutrition = 'strawberry'; //default value
}
現在您將獲得相同的結果http://example.com/nutrition/strawberry或http://example.com/nutrition/
uj5u.com熱心網友回復:
代碼對我有用我在這里發現了問題的型別
add_action( 'init', function() {
add_rewrite_rule('^(tenants)/([^/]*)/?', 'index.php?pagename=$matches[1]&property=$matches[2]','top');
add_rewrite_rule('^(buying/properties-for-sale-salisbury-2)/([^/]*)/?', 'index.php?pagename=$matches[1]&property=$matches[2]','top');
add_filter('query_vars', 'foo_my_query_vars');
});
function foo_my_query_vars($vars){
$vars[] = 'property';
return $vars;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342808.html
標籤:WordPress的 url重写
上一篇:MYSQLDB-隨機創建新表
