0

我无法让 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 函数一定有问题,但我想不通。

4

1 回答 1

0

多亏了对这个问题的令人难以置信的反应,我最终只是在没有 Wordpress 变量的帮助下让它工作。我刚刚将 Wordpress 配置为允许 url 中的变量,然后使用 PHP 获取 URL 并获取不同的 URL 段并分析它们。不如 Wordpress 方法漂亮,但至少它有效。

于 2012-11-12T14:43:19.250 回答