M
M
Max Zhukov2018-01-28 23:05:00
WordPress
Max Zhukov, 2018-01-28 23:05:00

How to fix pagination in Wordpress?

The content of the page of the custom post type does not change when switching pages to through pagination.
The pagination itself in the archive-mebel.php file:

<?php 

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
  'post_type'       => 'mebel',
  'posts_per_page'  => 2,
  'paged' => $paged
) ); 
$current_page = $page; ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php echo the_title(); ?>
<?php endwhile; ?>

<?php the_posts_pagination( array(
  'prev_text' => '« Назад',
  'next_text' => 'Вперед »',
  'screen_reader_text' => 'Страницы: ',
  'current'      => $current_page
) );
wp_reset_query(); ?>

I added this code so that it does not redirect to the 404 page
function codernote_request($query_string ) {
  if ( isset( $query_string['page'] ) ) {
    if ( ''!=$query_string['page'] ) {
      if ( isset( $query_string['name'] ) ) {
        unset( $query_string['name'] ); }
      }
    }
    return $query_string;
}
add_filter('request', 'codernote_request');

add_action('pre_get_posts', 'codernote_pre_get_posts');
function codernote_pre_get_posts( $query ) {
  if ( $query->is_main_query() && !$query->is_feed() && !is_admin() ) {
    $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) );
  }
}

get_query_var doesn't work returns nothing

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Ksyusha Terletskaya, 2018-01-30
@MaksZhukov

Try this one, it works for me

<?php 

  $custom_query_args = array(
      'post_type'=>'mebel',
      'posts_per_page' => 2
  );

  $custom_query_args['paged'] = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;

  $custom_query = new WP_Query( $custom_query_args );

  $temp_query = $wp_query;
  $wp_query   = NULL;
  $wp_query = $custom_query;


  if ( $custom_query->have_posts() ) :
      while ( $custom_query->have_posts() ) :
          $custom_query->the_post();
         
   			echo the_title(); 

      endwhile;
  endif;

  wp_reset_postdata();

  $args = array(
     'prev_text' => '« Назад',
      'next_text' => 'Вперед »',
      'screen_reader_text' => 'Страницы: '
  );
  the_posts_pagination($args);



  $wp_query = NULL;
  $wp_query = $temp_query;
?>

and in line
you need to register either get_query_var( 'page' ) - if the output is on a static main page, or get_query_var( 'paged' ) - for other pages.
I found the answer here https://wordpress.stackexchange.com/questions/1204...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question