我有一个自定义的帖子类型,叫做shop
其中包含产品帖子。此帖子类型连接到分类shop-category
, 指定车间内的产品部分。
每个产品都有一个item_width
和item_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_width
和
item_height
已设置查询参数。现在我想知道,当
item_width
和
item_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 );
最合适的回答,由SO网友:Robbert 整理而成
我为没有item_width
和item_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 );