Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination function doesn't work in WordPress

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".

like image 583
drake035 Avatar asked Sep 28 '17 12:09

drake035


People also ask

Why is pagination not working WordPress?

Look for improper code syntax if the code is there but pagination doesn't work. The line needs both the opening "lesser-than" sign (<), followed by a question mark. The code should close with a question mark, followed by a "greater-than" sign (>). If the code uses other variables, they should be well-formatted.


1 Answers

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;
like image 65
FluffyKitten Avatar answered Oct 04 '22 02:10

FluffyKitten