Timber pagination with custom post type
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.
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 TimberPostQuery
but from everything I've read, I shouldn't be doing this.
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.
Any tips would be appreciated.
pagination custom-post-type timber
add a comment |
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.
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 TimberPostQuery
but from everything I've read, I shouldn't be doing this.
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.
Any tips would be appreciated.
pagination custom-post-type timber
add a comment |
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.
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 TimberPostQuery
but from everything I've read, I shouldn't be doing this.
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.
Any tips would be appreciated.
pagination 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.
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 TimberPostQuery
but from everything I've read, I shouldn't be doing this.
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.
Any tips would be appreciated.
pagination custom-post-type timber
pagination custom-post-type timber
asked Nov 20 '18 at 20:28
JasmanJasman
939
939
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 TimberPostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
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);
});
}
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new TimberPost()
. 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.
In which PHP template file do you have the code where you call$myCollection = new TimberPostQuery($args);
?
– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I havetemplate-mycollection.php
andpage-mycollection.twig
. Depending on how mymycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look likewww.mysite.com/mycollections
, and then further pages would have/page/2
, etc.
– Jasman
Nov 21 '18 at 15:27
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.
– Jasman
Nov 28 '18 at 20:19
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401026%2ftimber-pagination-with-custom-post-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 TimberPostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
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);
});
}
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new TimberPost()
. 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.
In which PHP template file do you have the code where you call$myCollection = new TimberPostQuery($args);
?
– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I havetemplate-mycollection.php
andpage-mycollection.twig
. Depending on how mymycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look likewww.mysite.com/mycollections
, and then further pages would have/page/2
, etc.
– Jasman
Nov 21 '18 at 15:27
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.
– Jasman
Nov 28 '18 at 20:19
|
show 5 more comments
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 TimberPostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
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);
});
}
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new TimberPost()
. 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.
In which PHP template file do you have the code where you call$myCollection = new TimberPostQuery($args);
?
– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I havetemplate-mycollection.php
andpage-mycollection.twig
. Depending on how mymycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look likewww.mysite.com/mycollections
, and then further pages would have/page/2
, etc.
– Jasman
Nov 21 '18 at 15:27
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.
– Jasman
Nov 28 '18 at 20:19
|
show 5 more comments
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 TimberPostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
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);
});
}
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new TimberPost()
. 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.
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 TimberPostQuery($args);
$context['collection'] = $myCollection;
$context['pagination'] = $myCollection->pagination();
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);
});
}
I'm calling this function from the root of my functions.php
(i.e., not using any hook).
I should note that this also allows me to still get the page
in my context -- on every paginated page -- using $post = new TimberPost()
. 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.
edited Nov 21 '18 at 15:26
answered Nov 20 '18 at 21:47
JasmanJasman
939
939
In which PHP template file do you have the code where you call$myCollection = new TimberPostQuery($args);
?
– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I havetemplate-mycollection.php
andpage-mycollection.twig
. Depending on how mymycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look likewww.mysite.com/mycollections
, and then further pages would have/page/2
, etc.
– Jasman
Nov 21 '18 at 15:27
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.
– Jasman
Nov 28 '18 at 20:19
|
show 5 more comments
In which PHP template file do you have the code where you call$myCollection = new TimberPostQuery($args);
?
– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I havetemplate-mycollection.php
andpage-mycollection.twig
. Depending on how mymycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look likewww.mysite.com/mycollections
, and then further pages would have/page/2
, etc.
– Jasman
Nov 21 '18 at 15:27
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.
– Jasman
Nov 28 '18 at 20:19
In which PHP template file do you have the code where you call
$myCollection = new TimberPostQuery($args);
?– Gchtr
Nov 21 '18 at 15:25
In which PHP template file do you have the code where you call
$myCollection = new TimberPostQuery($args);
?– Gchtr
Nov 21 '18 at 15:25
This is a custom template, so, for example, I have
template-mycollection.php
and page-mycollection.twig
. Depending on how my mycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look like www.mysite.com/mycollections
, and then further pages would have /page/2
, etc.– Jasman
Nov 21 '18 at 15:27
This is a custom template, so, for example, I have
template-mycollection.php
and page-mycollection.twig
. Depending on how my mycollection
CPT is setup (e.g., using a slug rewrite), the route would probably look like www.mysite.com/mycollections
, and then further pages would have /page/2
, etc.– Jasman
Nov 21 '18 at 15:27
1
1
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
This is the reason why the default is not working then. Paginations only work in native archive pages, see this related answer. I have to admit this is not documented. It could definitely be improved!
– Gchtr
Nov 21 '18 at 15:47
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
@Gchtr, I had started to think about looking at the use of an archive page, but will have to decide whether this is acceptable for end users (may have to do with how the URL can or cannot be rewritten). I'm not a WP dev, so learning this as I go. Thanks for the input.
– Jasman
Nov 21 '18 at 16:22
1
1
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:
// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.– Jasman
Nov 28 '18 at 20:19
We are no longer having this issue. I believe the culprit was starting off by getting the pager value in our template (i.e., I've removed it). Per the instructions:
// now removed //global $paged; //if (!isset($paged) || !$paged) { $paged = 1; }
Too many changes to be sure, but at this point, pagination is working without any additional code outside of the template PHP.– Jasman
Nov 28 '18 at 20:19
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401026%2ftimber-pagination-with-custom-post-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown