首页 > 解决方案 > wordpress 自定义 add_rewrite_rule 不适用于分页

问题描述

我的 functions.php 文件中有以下函数。它的作用是让我可以一起使用 2 个自定义分类法。

function nfh_custom_rewrite_rules() {
   add_rewrite_rule('^product_cat/(.*)/bins/(.*)?', 'index.php?product_cat=$matches[1]&bins=$matches[2]', 'top');
}
add_action('init', 'nfh_custom_rewrite_rules');

所以我可以有这样的网址:

mysite.com/product_cat/homeware/bins/promo/

..这很好用

我遇到的问题是分页似乎不起作用。我一进入第 2 页:

mysite.com/product_cat/homeware/bins/promo/page/2/

它只是抛出一个未找到的错误。

任何帮助将不胜感激

标签: phpwordpressurl-rewriting

解决方案


我没有测试,但检查如下。这可能会有所帮助。

function nfh_custom_rewrite_rules() {
   add_rewrite_rule('^product_cat/(.*)/bins/(.*)/page/([0-9]{1,})/?', 'index.php?product_cat=$matches[1]&bins=$matches[2]&paged=$matches[3]', 'top');
}
add_action('init', 'nfh_custom_rewrite_rules');

推荐阅读