我无法让 Wordpress 使用 add_rewrite_rule() 函数为变量分配正确的值。我的目标是有一个如下所示的网址:www.mywebsite.com/catalog/category1/category2/item-slug
.
这是我在 functions.php 文件中的代码:
add_filter( 'query_vars', 'query_vars_new' );
function query_vars_new($query_vars){
$query_vars[]='category1';
$query_vars[]='category2';
$query_vars[]='catalog_item';
return $query_vars;
}
add_action( 'init', 'rewrite_init' );
function rewrite_init(){
add_rewrite_rule('catalog(/([^/]+))?(/([^/]+))?/?','index.php?page_id=94&category1=$matches[1]&category2=$matches[2]&catalog_item=$matches[3]','top');
}
所以目标是拥有三个变量$category1
, $category2
, 和$catalog_item
来自相应 url 段的值。但是,当我对此进行测试时,前两个变量设置为相同的值。
例如,www.mywebsite.com/catalog/clothes/shirts/polo-shirt
应该返回:
$category1="clothes";
$category2="shirts";
$catalog_item="polo-shirt";
但相反,我得到:
$category1="/clothes";
$category2="clothes";
$catalog_item="/polo-shirt";
我的 add_rewrite_rule 函数一定有问题,但我想不通。