我正在重建一个网站,其中要求我将归档页面的URL结构更改为WordPress默认情况下无法处理的内容。我有一个名为product, 具有自定义分类product-category. 我需要重写/product-category/{term-slug} 将此分类法的页面存档为/{term-slug}.
所以我想制定一个重写规则/{term-slug} 页面,并将其重写为/product-category/{term-slug}, 只有在没有其他规则匹配的情况下。
我可以这样做来捕获并正确重写这些页面:
function rewrite_product_archive() {
    add_rewrite_tag(\'%product-category%\', \'([a-z0-9\\-_]+)\');
    add_rewrite_rule(
        \'^([a-z0-9\\-_]+)/?$\',
        \'index.php?product-category=$matches[1]\',
        \'top\'
    );
}
add_action(\'init\', \'rewrite_product_archive\');
 有了这个,我可以导航到
/{term-slug} 页面,它将显示存档
/product-category/{term-slug} 正确翻页。然而,这现在导致所有实际页面停止工作,404。
所以我试着修改重写中的第三个参数top 到bottom, 还冲了一下。这使我的常规页面再次正常工作,但现在/{term-slug} 重写已停止工作,现在它们已达到404。
所以本质上,我只需要在没有其他规则匹配的情况下遵循重写。这可能吗?