The pagination function does not work in WordPress

Despite the fact that the example is in the code , the page to which I come when the link nextpagination <http: // localhost: 3000 / my_project / news / page / 2 /) does not exist ("page not found").

Why?

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; echo 'paged = ' . $paged;
$regular_posts = new WP_Query('posts_per_page=3&paged=' . $paged);
while ($regular_posts->have_posts()): $regular_posts->the_post(); 
  the_title();
endwhile;
echo get_next_posts_link('Older Entries', $regular_posts->max_num_pages);

This code is contained in my "home.php" template, which controls the "News" page that I created on the dashboard, and is set as the "Message Page" as the "Reading Settings".

+6
source share
4 answers

The static homepage is slightly different from the archive, as it is pageinstead paged.

Codex Pagination , ( ), :

if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }

, $paged :

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
+1

functions.php,

function pagination($pages = '', $range = 4) {
$showitems = ($range * 2) + 1;

global $paged;
if (empty($paged))
    $paged = 1;

if ($pages == '') {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if (!$pages) {
        $pages = 1;
    }
}

if (1 != $pages) {
    echo "<div class=\"fl-w-left main-pagination\">";
    if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
        echo "<a href='" . get_pagenum_link(1) . "'>&laquo; First</a>";
    if ($paged > 1 && $showitems < $pages)
        echo "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo; Previous</a>";
    echo '<ul>';
    for ($i = 1; $i <= $pages; $i++) {

        if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
            echo ($paged == $i) ? "<li class=\"active\">" . $i . "</li>" : "<a href='" . get_pagenum_link($i) . "'>" . $i . "</a>";
        }
    }
    echo '</ul>';

    if ($paged < $pages && $showitems < $pages)
        echo "<a href=\"" . get_pagenum_link($paged + 1) . "\">Next &rsaquo;</a>";
    if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
        echo "<a href='" . get_pagenum_link($pages) . "'>Last &raquo;</a>";
    echo "</div>\n";
}}

, ,

<?php if (function_exists("pagination")) { ?>
                    <?php pagination($post_query->max_num_pages); ?>
                <?php }
                ?>
0

$wp_query->posts_per_page WP_Query , $wp_query->max_num_pages WordPress , . , 404.php, home.php .

, $wp_query->max_num_pages, , , , . $wp_query->posts_per_page $wp_query->found_posts , $wp_query->max_num_pages.

, , - , . , functions.php:

function custom_main_query($query){
  if(is_admin()) return;
  if(!$query->is_main_query()) return;
  if(is_home()) $query->set('posts_per_page', 3);
}

add_action('pre_get_posts', 'custom_main_query');

, :

// Standard loop
while (have_posts()) : the_post();
  the_title();
endwhile;

// Navigation links
previous_posts_link('Newer Entries');
next_posts_link('Older Entries');

, , , Options => Reading WordPress Blog pages show at most 3.

0

This error usually occurs due to the limits set for the number of messages displayed on your templates. This value may be less than 4. Go to your WordPress toolbar and go to Settings->Reading. Look at the blog pages no more , and if the value is less than 4, increase it to the total number of posts that will be displayed on your template, plus a link for pagination (in your case, it should be at least 4).

0
source

All Articles