Pagination function doesn't work in WordPress

2019-04-29 01:41发布

Despite following exactly the example in the codex, the page I arrive at when clicking the pagination next link (http://localhost:3000/my_project/news/page/2/) doesn't 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, managing the "News" page which I created in dashboard and set as "Posts page" in "Reading Settings".

4条回答
Summer. ? 凉城
2楼-- · 2019-04-29 02:18

Changing $wp_query->posts_per_page in the template with a new instance of WP_Query doesn't work because the requested page number has already been validated against $wp_query->max_num_pages by WordPress before the template is executed. As the requested page is more than this value for the main query, the 404.php template is served instead, and your code in the home.php template isn't even executed.

The trick is to get $wp_query->max_num_pages to match the true maximum number of pages by manipulating the main query to use your custom arguments, before the main query is performed. $wp_query->posts_per_page and $wp_query->found_posts will then match your results, resulting in a $wp_query->max_num_pages that does too.

The best way, in my opinion, is to edit the main query arguments before the main query gets its posts. For example, you could add the following to your theme's 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');

This will then mean a standard loop will work out the box too:

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

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

If you are only manipulating the number of posts per page, it may be better to change the default posts per page by going to Options => Reading in WordPress admin and setting Blog pages show at most to 3 here instead.

查看更多
闹够了就滚
3楼-- · 2019-04-29 02:31

You can put this code in function.php file,

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";
}}

Also, put this code in your page or post template file,

<?php if (function_exists("pagination")) { ?>
                    <?php pagination($post_query->max_num_pages); ?>
                <?php }
                ?>
查看更多
Juvenile、少年°
4楼-- · 2019-04-29 02:39

This error usually occurs due to limits set on the number of posts to show on your templates. It's possible this is set to less than 4. So get to your WordPress dashboard and navigate to Settings->Reading. Look for Blog pages show at most and if the value is less than 4, increase it to the total number of posts to show on your template, plus the pagination link (in your case, this should be at least 4).

查看更多
Lonely孤独者°
5楼-- · 2019-04-29 02:40

A static homepage is slightly different from an archive, as it page parameter instead of paged.

The Codex Pagination page includes this code for static homepages, which will actually work in all cases (i.e. even archive pages) because its checking for both parameters:

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

But if you only need it to work on the homepage, Changing your code for the $paged variable to the following should work too:

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
查看更多
登录 后发表回答