在不同URL级别上重写规则分页

时间:2016-05-03 作者:Robbert

我有一个自定义的帖子类型,叫做shop 其中包含产品帖子。此帖子类型连接到分类shop-category, 指定车间内的产品部分。

每个产品都有一个item_widthitem_height 自定义字段。正如您在下面看到的,我制作了一些URL,以按宽度和高度过滤产品:

https://www.example.com/shop-category/product/
https://www.example.com/shop-category/product/80/
https://www.example.com/shop-category/product/80/200/
https://www.example.com/shop-category/product/80/200/page/2/
当您使用分页导航到产品的下一页时,将设置最后url(/页/2/)的最后两个级别。目前,您只能在item_widthitem_height 已设置查询参数。现在我想知道,当item_widthitem_height 未设置。

例如,使用以下url:

https://www.example.com/shop-category/product/page/2/

Added variables and added rewrite rules:

// Adds query vars for shop post type
function gtp_add_shop_query_vars( $vars ) {
    $vars[] = \'shop-category\';
    $vars[] = \'item_width\';
    $vars[] = \'item_height\';
    return $vars;       
}
add_filter( \'query_vars\', \'gtp_add_shop_query_vars\' );

// Adds rewrite rules for shop post type
function gtp_add_shop_rewrite_rules() {
    add_rewrite_rule( \'^shop-category/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]&page=$matches[4]&paged=$matches[5]\', \'top\' );
    add_rewrite_rule( \'^shop-category/([^/]*)/([^/]*)/([^/]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]\', \'top\' );
    add_rewrite_rule( \'^shop-category/([^/]*)/([^/]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]\', \'top\' );
    add_rewrite_rule( \'^shop-category/([^/]*)$\', \'index.php?shop-category=$matches[1]\', \'top\' );
}
add_action( \'init\', \'gtp_add_shop_rewrite_rules\', 10, 0 );

1 个回复
最合适的回答,由SO网友:Robbert 整理而成

我为没有item_widthitem_height 结合分页,如下所示:

/** 
 * Adds rewrite rules for shop category
 */  
function gtp_add_shop_rewrite_rules() {

    // shop-category/category/item_width/item_height/page/2/
    add_rewrite_rule( \'^shop-category\\/([^/]*)\\/([^/]*)\\/([^/]*)\\/page\\/([0-9]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]&paged=$matches[4]\', \'top\' );

    // shop-category/category/page/2/
    add_rewrite_rule( \'^shop-category\\/([^/]*)\\/page\\/([0-9]*)$\', \'index.php?shop-category=$matches[1]&paged=$matches[2]\', \'top\' );

    // /shop-category/category/item_width/item_height/
    add_rewrite_rule( \'^shop-category\\/([^/]*)\\/([^/]*)\\/([^/]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]\', \'top\' );

    // /shop-category/category/item_width/
    add_rewrite_rule( \'^shop-category\\/([^/]*)\\/([^/]*)$\', \'index.php?shop-category=$matches[1]&item_width=$matches[2]\', \'top\' );

}
add_action( \'init\', \'gtp_add_shop_rewrite_rules\', 10, 0 );

相关推荐

Conditional action hooks

我正在开发一个使用AJAX的插件,但我在控制代码流方面遇到了困难。我想在运行条件后挂接函数。我的钩子在控制结构内部添加时不会启动,但在外部会启动。我还有一个触发AJAX请求的事件,这只能在用户点击触发事件后,在加载所有DOM之后发生。AJAX请求告诉PHP函数设置cookie。它设置了cookie,但在阅读关于cookie的PHP文档时,我发现它们只能在发送任何输出后设置,但我还是做到了这一点?我读过WordPress初始化序列,但这并没有帮助我解决这个问题。这是我的密码:add_action(\'wp_