Php 分页WPQuery重定向到第1页
在我的Wordpress模板中,我正在进行WP查询,以便列出特定地区的所有城市(自定义帖子类型)。 在某些情况下,地区有3000多个城市,因此查询太重,内存不足。 因此,我使用paginate_links函数,以便在结果超过500个时对每个列表进行分页。 它工作正常,但分页链接以某种方式重定向到主页 示例: 重定向到 所以我永远无法访问分页的页面 这是我的代码(部分)。 你知道是什么导致了这个问题吗Php 分页WPQuery重定向到第1页,php,wordpress,pagination,Php,Wordpress,Pagination,在我的Wordpress模板中,我正在进行WP查询,以便列出特定地区的所有城市(自定义帖子类型)。 在某些情况下,地区有3000多个城市,因此查询太重,内存不足。 因此,我使用paginate_links函数,以便在结果超过500个时对每个列表进行分页。 它工作正常,但分页链接以某种方式重定向到主页 示例: 重定向到 所以我永远无法访问分页的页面 这是我的代码(部分)。 你知道是什么导致了这个问题吗 <?php $paged = ( get_query_var( 'paged
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = array(
'post_type' => 'commune',
'posts_per_page'=> 500,
'paged' => $paged,
'orderby' => 'title',
'order'=> 'ASC',
'meta_key' => 'region',
'meta_value' => $reg_region
);
$communes = new WP_Query($posts );
$nb_communes = $communes-> found_posts;
?>
<div class="col-8 main-content">
<?php while (have_posts()) : the_post(); ?>
<?php
echo "<ul class=\"list-group\">";
while ($communes->have_posts() ) { $communes->the_post();
?>
<li class="list-group-item"><?php echo(get_field("commune")); ?> (<?php echo(get_field("cp")); ?>) - <a href="<?php echo get_permalink(); ?>"> <?php echo(get_field("civilite")); ?> <?php echo(get_field("prenom")); ?> <?php echo(get_field("nom")); ?> </a> </li>
<?php
}
echo "</ul>";
?>
<?php endwhile; // end of the loop. ?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $communes->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 500,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Précédent', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Suivant', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
/**
* Remove the slug from published post permalinks. Only affect our custom post type, though.
*/
function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
$custom_post_types = array('departement', 'region', 'commune');
if ( ! in_array( $post->post_type, $custom_post_types ) || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
/**
* Have WordPress match postname to any of our public post types (post, page, race)
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* By default, core only accounts for posts and pages where the slug is /post-name/
*/
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array('post', 'departement', 'region', 'commune' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );