本文介绍了分页功能在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分页" 页包含用于静态首页的此代码,该代码实际上可以在所有案例(甚至是存档页面),因为它同时检查了两个参数:

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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:41