A
A
alexander_chn2018-09-23 12:12:21
WordPress
alexander_chn, 2018-09-23 12:12:21

Why is pagination not working properly in Wordpress?

Hello!
This is how I post articles:

$i = 0;
$arr = array();

$count_items = 12;
$paged = get_query_var('paged', 1);
$recent = new WP_Query( "posts_per_page=$count_items&paged=$paged" );

while ( $recent->have_posts() ):
  $recent->the_post();
  $category = get_the_category();

  $arr[$i]['link']    = get_permalink();
  $arr[$i]['img']     = return_the_post_thumbnail_url('widgetfull');
  $arr[$i]['rubrika'] = $category[0]->cat_name;
  $arr[$i]['title']   = get_the_title();
  $arr[$i]['excerpt'] = do_excerpt(get_the_excerpt(), 17);

  $i++;
endwhile;
wp_reset_postdata();
print_r($arr);

Below I place:
wp_corenavi();
The problem is that it displays 12 pages for pagination, but the articles end on page 9. And the other three pages are blank. How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Pupkin, 2018-09-24
@alexander_chn

For a custom request, you need the same paginator. Type:

...
<?php endwhile; ?>
<?php if ($recent->max_num_pages > 1) : // custom pagination  ?>
    <?php
    $orig_query = $wp_query;
    $wp_query = $recent;
    ?>
    <nav class="prev-next-posts">
        <div class="prev-posts-link">
            <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
        </div>
        <div class="next-posts-link">
            <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
        </div>
    </nav>
    <?php
    $wp_query = $orig_query;
    ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question