分页功能在 WordPress 中不起作用 [英] Pagination function doesn't work in WordPress

查看:38
本文介绍了分页功能在 WordPress 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管完全遵循代码中的示例,但我点击分页时到达的页面next 链接(http://localhost:3000/my_project/news/page/2/) 不存在(找不到页面").

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

为什么?

$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);

此代码包含在我的home.php"模板中,管理我在仪表板中创建并在阅读设置"中设置为帖子页面"的新闻"页面.

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

推荐答案

静态主页与存档略有不同,因为它是 page 参数而不是 paged.

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

Codex Pagination 页面包含用于静态主页的此代码,它实际上适用于所有案例(即甚至存档页面),因为它检查两个参数:

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

但如果您只需要在主页上使用它,将 $paged 变量的代码更改为以下内容也应该可以:

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;

这篇关于分页功能在 WordPress 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆