[英]Pagination doesn't work with custom rewrite rules
I'm trying to show discounted products on Woocommerce at http://example.com/shop/discounted
which works fine as long as there is just one page, but when pagination comes in, it doesn't work, and var_dump( get_query_var('paged')
always returns 0
, i have defined two rewrite rules, one for when there is no pagination and one for when user navigates to next pages. 我想在
http://example.com/shop/discounted
上的Woocommerce上展示打折产品,只要只有一页,它就可以正常工作,但是当出现分页时,它不起作用,并且var_dump( get_query_var('paged')
始终返回0
,我定义了两个重写规则,一个用于没有分页的规则,一个用于用户导航到下一页的规则。
function dw_custom_rewrite_rules() {
add_rewrite_endpoint( 'sortby', EP_ALL_ARCHIVES );
add_rewrite_rule('^shop/discounted?', 'index.php?post_type=product&sortby=discounts', 'top');
// With pagination
add_rewrite_rule('^shop/discounted/page/?([0-9]{1,})/?$', 'index.php?post_type=product&sortby=discounts&paged=$matches[1]', 'top');
}
add_action('init', 'dw_custom_rewrite_rules');
I finally did manage to fix this, Thanks to this answer , i could'nt use the keyword paged
i don't know why, so i just changed it to pageds
, and then adding a little bit of extra code to the pre_get_posts
hook ( with your own coditionals ofcourse ) : 我终于还是设法解决这个问题,多亏了这个答案 ,我could'nt使用关键字
paged
我不知道为什么,所以我只是把它改成pageds
,再加入额外的代码到一点点pre_get_posts
钩(与您自己的课程组合):
add_action('pre_get_posts', function( $query ){
$paged = (int) get_query_var( 'pageds', 1 );
$query->set('paged', $paged);
});
then adding pageds
to query_vars
: 然后将
pageds
添加到query_vars
:
add_filter('query_vars', function( $vars ){
$vars[] = 'pageds';
return $vars;
});
and one more thing is we don't have to write two seperate rewrite rules, we can combine them like this : 还有一件事是我们不必编写两个单独的重写规则,我们可以像这样组合它们:
function dw_custom_rewrite_rules() {
add_rewrite_endpoint( 'sortby', EP_ALL_ARCHIVES );
add_rewrite_rule('^shop/discounted(/page/([0-9]+)?)?/?$', 'index.php?post_type=product&sortby=discounts&pageds=$matches[2]', 'top');
}
add_action('init', 'dw_custom_rewrite_rules');
I don't use Wordpress, but usually you will never evaluate $matches[1] with single quotes. 我不使用Wordpress,但是通常您永远都不会使用单引号评估$ matches [1]。 Try double quotes instead and check what does your variable output.
请尝试使用双引号,然后检查变量输出是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.