[英]Wordpress pagination error with query strings
我有一个带有自定义WP查询的页面,用于输出与奖励帖子类型相关的不同帖子。
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 2,
'ignore_sticky_posts' => 1,
'post_type' => 'award',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
's' => $_GET['keyword'],
'paged' => $paged;
);
我正在使用默认的wordpress分页功能
paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => $paged,
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'mid_size' => 3,
'prev_next' => false,
'add_args' => false,
'add_fragment' => '',
));
如果我提交这个url awards/?keyword=test
,我会得到几个页码的相关结果。 如果我点击分页上的第2页,页面会重定向到此网址awards/page/2/?keyword=test
,我找不到页面错误! 如果我访问awards/page/2/
也会遇到同样的错误! 我究竟做错了什么?
如果我可以在URL中保留我的查询字符串,例如这样的awards/?page=2&keyword=test
那也是很好的。
UPDATE
如果删除奖励自定义帖子类型但我将帖子保留在数据库中,则过滤器的分页可以正常工作。
此外,WP查询位于名为“奖赏”的页面中,其中包含一系列奖赏。
我的奖励自定义帖子类型:
$cpt = array(
'singular' => 'Article',
'plural' => 'Awards & Press',
'type' => 'award',
'slug' => 'awards'
);
register_post_type( $cpt['type'],
array(
'labels' => array(
'name' => sprintf( '%s', $cpt['plural'] ),
'singular_name' => sprintf( '%s', $cpt['singular'] ),
'add_new' => 'Add New',
'add_new_item' => sprintf( 'Add New %s', $cpt['singular'] ),
'edit' => 'Edit',
'edit_item' => sprintf( 'Edit %s', $cpt['singular'] ),
'new_item' => sprintf( 'New %s', $cpt['singular'] ),
'all_items' => sprintf( 'All %s', $cpt['plural'] ),
'view' => sprintf( 'View %s', $cpt['singular'] ),
'search_items' => sprintf( 'Search %s', $cpt['plural'] ),
'not_found' => sprintf( 'No %s Found', $cpt['plural'] ),
'not_found_in_trash' => sprintf( 'No %s Found In Trash', $cpt['plural'] ),
'parent_item_colon' => ''
),
'description' => 'Create a new item',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'publicly_queryable' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => $cpt['slug'],
'with_front' => false,
),
'menu_position' => 5,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'query_var' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
)
)
);
另一个更新
如果我改变:
'rewrite' => array(
'slug' => $cpt['slug'],
'with_front' => false,
),
至
'rewrite' => false,
过滤器的分页工作,但我失去了漂亮的网址awards/post-name/
它成为awards/?award=post-name
。
我需要对过滤器进行分页,同时保持帖子的漂亮网址。
我相信我找到了答案,但如果其他人能证实这是最好的解决方案,我会很高兴:
add_action('init', function() {
add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
});
我将上面的代码添加到functions.php文件中。
解决方案来自这里: https : //www.grzegorowski.com/wordpress-rewrite-rules/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.