简体   繁体   English

分页和重写规则使Wor​​dPress出现问题

[英]Pagination and rewrite rules make problems in WordPress

Wi have custom page on custom page template which should list user posts and this works but i have query arg that should filter between published and unpublished (draft) posts. Wi在自定义页面模板上有一个自定义页面,该页面应列出用户帖子,这可行,但是我有查询arg,应在已发布和未发布(草稿)帖子之间进行过滤。 On top of that well it should have pagination so it only shows 10 posts per page. 在该孔的顶部,它应具有分页功能,因此每页仅显示10个帖子。

The problem exists when i want to list all posts and go to second page. 当我要列出所有帖子并转到第二页时,存在问题。

To be able to browse by filters and have pagination i had made custom rewrite and it works but without filters pagination only gives first page. 为了能够通过过滤器进行浏览并进行分页,我进行了自定义重写,它可以正常工作,但没有过滤器的分页只会显示首页。

To better understand this is how it works. 为了更好地理解这是如何工作的。

mywebsite.com/my-posts/ <---- this works mywebsite.com/my-posts/ <----这行得通

mywebsite.com/my-posts/published/ <---- this works mywebsite.com/my-posts/published/ <----这行得通

mywebsite.com/my-posts/published/page/2 <---- this works mywebsite.com/my-posts/published/page/2 <---- mywebsite.com/my-posts/published/page/2

mywebsite.com/my-posts/page/2 <---- this DO NOT work mywebsite.com/my-posts/page/2 <----这不起作用

And without pretty permalinks structure is like this 而且没有漂亮的永久链接结构是这样的

mywebsite.com/?page_id=23

mywebsite.com/?page_id=23&filter=published

mywebsite.com/?page_id=23&filter=published&paged=2

mywebsite.com/?page_id=23&paged=2

Now if i do not use pretty permalinks it works but something i am missing with rewrite and i can't figure it out. 现在,如果我不使用漂亮的永久链接,它可以工作,但是我缺少一些重写功能,因此无法解决。

function custom_rewrite_rule() {
    add_rewrite_tag('%filter%', '([^&]+)');

    // add_rewrite_rule(get_post_field( 'post_name', get_theme_option('user_link') ) . '/([^/]+)/page/([0-9]{1,})/?','index.php?page_id='. get_theme_option('user_link') .'&user=$matches[1]&paged=$matches[2]','top');
    // add_rewrite_rule(get_post_field( 'post_name', get_theme_option('user_link') ) . '/([^/]+)/?','index.php?page_id='. get_theme_option('user_link') .'&user=$matches[1]','top');
    // add_rewrite_tag('%user%', '([^&]+)');

    add_rewrite_rule(get_post_field( 'post_name', get_theme_option('myprofile_page') ) . '/([^/]+)/?','index.php?page_id='. get_theme_option('myprofile_page') .'&filter=$matches[1]','top');
    add_rewrite_rule(get_post_field( 'post_name', get_theme_option('myprofile_page') ) . '/([^/]+)/page/([0-9]{1,})/?','index.php?page_id='. get_theme_option('myprofile_page') .'&filter=$matches[1]&paged=$matches[2]','bottom');
    add_rewrite_rule(get_post_field( 'post_name', get_theme_option('myprofile_page') ) . '/page/([0-9]{1,})/?','index.php?page_id='. get_theme_option('myprofile_page') .'&paged=$matches[1]','bottom');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

function custom_query_vars( $vars ) {
    array_push($vars, 'filter');
    return $vars;
}
add_filter( 'query_vars', 'custom_query_vars' );

I know this one paged with filter interferes with one another but this is the last solution i tried and it's not working. 我知道这一个带有filter paged互相干扰,但这是我尝试的最后一个解决方案,它不起作用。

Well to answer my own question if anyone else get stuck on something like this, it was really simple, i was missing only $ at the end of regex to get it to work. 好回答我自己的问题,如果还有其他人遇到类似问题,这确实很简单,我在正则表达式末尾只缺少$即可使它正常工作。

add_rewrite_rule( get_post_field( 'post_name', get_theme_option('myprofile_page') ) . '/([^/]+)/?$', 'index.php?page_id='. get_theme_option('myprofile_page') .'&filter=$matches[1]','top');
add_rewrite_rule( get_post_field( 'post_name', get_theme_option('myprofile_page') ) . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?page_id='. get_theme_option('myprofile_page') .'&filter=$matches[1]&paged=$matches[2]','top');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM