Php 具有多个URL参数的Wordpress分页格式
我正在致力于Wordpress主题的开发和实现分页。 我将URL格式更改为Php 具有多个URL参数的Wordpress分页格式,php,wordpress,filter,pagination,Php,Wordpress,Filter,Pagination,我正在致力于Wordpress主题的开发和实现分页。 我将URL格式更改为http://localhost:10023/achievement?paged=2 但是现在我有了一个过滤器,它带有另一个名为地区的参数,比如http://localhost:10023/achievement?prefecture=tokushima 当我转到下一页时,URL是http://localhost:10023/achievement?prefecture=tokushima?paged=2,包含两个?s,因
http://localhost:10023/achievement?paged=2
但是现在我有了一个过滤器,它带有另一个名为地区
的参数,比如http://localhost:10023/achievement?prefecture=tokushima
当我转到下一页时,URL是http://localhost:10023/achievement?prefecture=tokushima?paged=2
,包含两个?
s,因此查询返回错误的数据
当URL有其他参数时,我需要将?paged=2
替换为&paged=2
我的代码如下。
谢谢你
pagination.php
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
the_posts_pagination(array(
// 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'base' => get_pagenum_link(1) . '%_%',
'format'=> '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'screen_reader_text' => ' ',
'prev_text' => __( '<i class="fa fa-arrow-left arrow"></i>', 'textdomain' ),
'next_text' => __( '<i class="fa fa-arrow-right arrow"></i>', 'textdomain' ),
'class' => 'custom-pagination',
));
?>
// Prevent page redirect for the pagination
function my_disable_redirect_canonical( $redirect_url ) {
if ( is_archive() ){
$subject = $redirect_url;
$pattern = '/\/page\//'; // Check URL if contains /page/
preg_match($pattern, $subject, $matches);
if ($matches){
$redirect_url = false;
return $redirect_url;
}
}
}
add_filter('redirect_canonical','my_disable_redirect_canonical');
function achievement_archive_filter ($query) {
if(!is_archive() && get_post_type() !== 'achievement') return;
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'achievement' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'achievement' ) {
// allow the url to alter the query
if( isset($_GET['prefecture']) ) {
$query->set('meta_key', 'prefecture');
$query->set('meta_value', $_GET['prefecture']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'achievement_archive_filter');