admin管理员组文章数量:1130349
I have a custom post type of players, and archive-players.php lists them all at an address such as www.site/players.
I'm then using a url like www.site/players?type=pro to list another page of just the pro players (modified wp_query based on custom user role, hence why I don't just do this with a taxonomy). I decided I should rewrite the url so it's www.site/players/pro.
The page itself works fine, but my question is: how can I now fix the pagination when the url is something like www.site/players/type/pro/page/3/? at the moment I'm getting page not found. I'm using wp page-navi for the pagination. In fact, pagination doesn't work when I have it as just ?type=pro either.
The code I have for the rewrite is below. Is it better to rewrite using wordpress or mod_rewrite? I have no experience with either. Am I going about this the right way? I've read some other questions and posts all over the place, but the whole thing just confuses me.
function add_query_vars($aVars) {
$aVars[] = "type";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array('players/type/([^/]+)/?$' => 'index.php?post_type=players&type=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
I have a custom post type of players, and archive-players.php lists them all at an address such as www.site/players.
I'm then using a url like www.site/players?type=pro to list another page of just the pro players (modified wp_query based on custom user role, hence why I don't just do this with a taxonomy). I decided I should rewrite the url so it's www.site/players/pro.
The page itself works fine, but my question is: how can I now fix the pagination when the url is something like www.site/players/type/pro/page/3/? at the moment I'm getting page not found. I'm using wp page-navi for the pagination. In fact, pagination doesn't work when I have it as just ?type=pro either.
The code I have for the rewrite is below. Is it better to rewrite using wordpress or mod_rewrite? I have no experience with either. Am I going about this the right way? I've read some other questions and posts all over the place, but the whole thing just confuses me.
function add_query_vars($aVars) {
$aVars[] = "type";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array('players/type/([^/]+)/?$' => 'index.php?post_type=players&type=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
Share
Improve this question
asked Jul 16, 2011 at 13:20
AndrewAndrew
1,1633 gold badges24 silver badges47 bronze badges
2 Answers
Reset to default 11You have to add another rule which maps the /page/# to the &paged=# variable.
Really though, you're kinda doing it all backwards here by filtering the rules array. Just calling add_rewrite_rule to add your rules makes a bit more sense.
function plugin_name_add_rewrite_rules() {
add_rewrite_rule('players/type/([^/]+)/?$',
'index.php?post_type=players&type=$matches[1]',
'top');
add_rewrite_rule('players/type/([^/]+)/page/([0-9]+)?$',
'index.php?post_type=players&type=$matches[1]&paged=$matches[2]',
'top');
}
add_filter('init', 'plugin_name_add_rewrite_rules');
I can't comment, but the selected answer seems out of date. With the latest Wordpress version, rules seem to be checked in order so you need to add the pagination rewrite rule first to get this working.
e.g.
function plugin_name_add_rewrite_rules() {
add_rewrite_rule('players/type/([^/]+)/page/([0-9]+)?$','index.php?post_type=players&type=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule('players/type/([^/]+)/?$','index.php?post_type=players&type=$matches[1]','top');
}
add_filter('init', 'plugin_name_add_rewrite_rules');
I have a custom post type of players, and archive-players.php lists them all at an address such as www.site/players.
I'm then using a url like www.site/players?type=pro to list another page of just the pro players (modified wp_query based on custom user role, hence why I don't just do this with a taxonomy). I decided I should rewrite the url so it's www.site/players/pro.
The page itself works fine, but my question is: how can I now fix the pagination when the url is something like www.site/players/type/pro/page/3/? at the moment I'm getting page not found. I'm using wp page-navi for the pagination. In fact, pagination doesn't work when I have it as just ?type=pro either.
The code I have for the rewrite is below. Is it better to rewrite using wordpress or mod_rewrite? I have no experience with either. Am I going about this the right way? I've read some other questions and posts all over the place, but the whole thing just confuses me.
function add_query_vars($aVars) {
$aVars[] = "type";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array('players/type/([^/]+)/?$' => 'index.php?post_type=players&type=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
I have a custom post type of players, and archive-players.php lists them all at an address such as www.site/players.
I'm then using a url like www.site/players?type=pro to list another page of just the pro players (modified wp_query based on custom user role, hence why I don't just do this with a taxonomy). I decided I should rewrite the url so it's www.site/players/pro.
The page itself works fine, but my question is: how can I now fix the pagination when the url is something like www.site/players/type/pro/page/3/? at the moment I'm getting page not found. I'm using wp page-navi for the pagination. In fact, pagination doesn't work when I have it as just ?type=pro either.
The code I have for the rewrite is below. Is it better to rewrite using wordpress or mod_rewrite? I have no experience with either. Am I going about this the right way? I've read some other questions and posts all over the place, but the whole thing just confuses me.
function add_query_vars($aVars) {
$aVars[] = "type";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array('players/type/([^/]+)/?$' => 'index.php?post_type=players&type=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
Share
Improve this question
asked Jul 16, 2011 at 13:20
AndrewAndrew
1,1633 gold badges24 silver badges47 bronze badges
2 Answers
Reset to default 11You have to add another rule which maps the /page/# to the &paged=# variable.
Really though, you're kinda doing it all backwards here by filtering the rules array. Just calling add_rewrite_rule to add your rules makes a bit more sense.
function plugin_name_add_rewrite_rules() {
add_rewrite_rule('players/type/([^/]+)/?$',
'index.php?post_type=players&type=$matches[1]',
'top');
add_rewrite_rule('players/type/([^/]+)/page/([0-9]+)?$',
'index.php?post_type=players&type=$matches[1]&paged=$matches[2]',
'top');
}
add_filter('init', 'plugin_name_add_rewrite_rules');
I can't comment, but the selected answer seems out of date. With the latest Wordpress version, rules seem to be checked in order so you need to add the pagination rewrite rule first to get this working.
e.g.
function plugin_name_add_rewrite_rules() {
add_rewrite_rule('players/type/([^/]+)/page/([0-9]+)?$','index.php?post_type=players&type=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule('players/type/([^/]+)/?$','index.php?post_type=players&type=$matches[1]','top');
}
add_filter('init', 'plugin_name_add_rewrite_rules');
本文标签: rewrite rulesHow to fix pagination after rewriting url ie wwwsitecomplayerstypepropage3
版权声明:本文标题:rewrite rules - How to fix pagination after rewriting url? ie. www.site.complayerstypepropage3 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749103393a2316355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论