I
I
IInspectorGadget2020-06-07 21:26:07
WordPress
IInspectorGadget, 2020-06-07 21:26:07

Why does pagination not work and pages ../page/2 do not work in wordpress?

I created a custom type of post "Hot tour", you need to display all his posts on the page page by page.
Pagination is not displayed.

<?php
global $wp_query;
$page = get_query_var('paged');
$args = array (
'post_type'   => 'hot-tour',
  'posts_per_page'         => '2',
  'paged' => $page,
);

$custom_query = new WP_Query( $args );


if ( $custom_query->have_posts() ) {
  while ( $custom_query->have_posts() ) {
    $custom_query->the_post();
                the_title();
  }
}
posts_nav_link()
?>
<?php get_footer() ;?>


In the admin there is a conclusion of 2 posts.

Also, if I manually try to go to another page ../page/2 - transfers to index.php

register_post_type( 'hot-tour', [
    'label'  => 'Горящий тур',
    'public'              => true,
    'menu_position'       => 4,
        'supports'            => ['title' ,'thumbnail', 'custom-fields' ], // 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'
        'has_archive' => true,
        
  ] );


Help solve the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Sedov, 2020-06-09
@ivansedov

$query = new WP_Query([
  'post_type'      => 'hot-tour',
  'posts_per_page' => 2,
  'paged'          => get_query_var('page'),
]);

if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    the_title();
  }

  wp_reset_postdata();
}

// Выводим пагинацию
echo paginate_links([
  'base'    => user_trailingslashit(wp_normalize_path(get_permalink() . '/%#%/')),
  'current' => max(1, get_query_var('page')),
  'total'   => $query->max_num_pages,
]);

And read how to correctly display pagination in the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question