具有自定义帖子类型的木材分页 [英] Timber pagination with custom post type
问题描述
我正在尝试将Timber文档中显示的示例用于使用自定义帖子类型的分页.这不符合我的预期.
I am trying to use the example shown in the Timber docs for pagination with a custom post type. This is not working as I'd expect.
我要进行分页输出的唯一方法是在设置查询 $ args
之后以及通过它们之前添加 query_posts($ args);
放入一个新的 Timber \ PostQuery
中,但是从我读过的所有内容来看,我都不应该这样做.
The only way I've gotten a pagination to output is to add query_posts($args);
after I set up my query $args
and before I pass them into a new Timber\PostQuery
but from everything I've read, I shouldn't be doing this.
我已阅读问题线程,但它似乎依赖于使用 WP_Query
直接,而我正在尝试使此任务尽可能简单.
I have read this issue thread, but it seems to rely on using WP_Query
directly, and I'm trying to keep this task as simple as possible.
任何提示将不胜感激.
推荐答案
好吧,我看不到它的记录位置,但是这种结构似乎对我有用(调用 pagination()
在我的CPT收藏夹上):
Well, I can't see where this is documented, but this structure seems to have worked for me (calling pagination()
on my CPT collection):
$args = array(
'post_type' => 'my_custom_post_type',
'posts_per_page' => 5,
'paged' => $paged
);
$myCollection = new Timber\PostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
然后,我必须设置> Routes :: map
使分页真正起作用.这是一个示例:
Then I have to set up Routes::map
for the pagination to actually work. Here's an example:
function add_routes() {
Routes::map(':name/page/:pg', function($params){
$query = 'posts_per_page=1&post_type=page&paged='.$params['pg'];
Routes::load('template-'.$params['name'].'.php', $params, $query);
});
}
我要从我的 functions.php
的根调用此函数(即,不使用任何钩子).
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
我应该注意,这也使我仍然可以在上下文中-在每个分页页面上使用 $ post = new Timber \ Post()
.那里的关键似乎是使用 posts_per_page = 1
和 post_type = page
设置路线.最初,我通过为模板中的分页CPT设置与我的查询 $ args
相同的路由映射来使自己感到困惑.
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new Timber\Post()
. The key there seems to be setting up the route with posts_per_page=1
and post_type=page
. Originally, I was confusing myself by setting up the route mapping the same as my query $args
for paginated CPTs in my template.
这篇关于具有自定义帖子类型的木材分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!