木材 - 分页不使用默认WordPress查询
我正在尝试在博客页面上的默认查询中显示分页。 因此,我在page-home中使用此代码的文件称为home.php
<?php
use Timber\Post;
use Timber\Timber;
$post = new Post();
$context = Timber::context();
$context['post'] = $post;
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args = [
'post_staus' => 'published',
'posts_per_page' => 2,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'offset' => 1,
];
$context['posts'] = Timber::get_posts($args);
$argsNewest = [
'posts_per_page' => 1,
'offset' => 0,
];
$context['latest'] = Timber::get_posts($argsNewest);
Timber::render('page-home.twig', $context);
。我试图显示我的分页,但没有任何成功。
{% include 'partial/pagination.twig' with { pagination: posts.pagination({show_all: false, mid_size: 3, end_size: 2}) } %}
另一个奇怪的是,当我尝试转让诸如 {{dump(ports)}}
之类的帖子时,它无法正常工作,但是lop like {%for post in posts%}
工作正常,我可以显示帖子。
所以我的问题:
- 为什么我的分页不起作用?
- 为什么我的垃圾场不起作用,而是循环是?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
function.php
文件中,添加此代码(或附加到现有的pre_get_posts filter)您可能需要在代码顶部添加
使用timber \ postquery;
。在您的home.php文件中,替换
为
关于如何在树枝中使用分页的信息。
参见 htttps://developer.wordpress.orge.org/reference.orperion/reference/hooks/hooks/pre_get_posts/pre_pet_posts/ 有关PRE_GET_POSTS过滤器的更多信息。
In your
functions.php
file, add this code (or append to existing pre_get_posts filter)You might need to add
use Timber\PostQuery;
at the top of your code.In your home.php file, replace
with
See https://timber.github.io/docs/guides/pagination/ on how to use pagination in Twig.
See https://developer.wordpress.org/reference/hooks/pre_get_posts/ for more information on the pre_get_posts filter.