Is it possible to get url of first and last posts with Handlebars?


#1

I working on a new theme, and I need to find a way to get the URL of the first and last posts (the same way next_post prev_post works). I’d like to do this with handlebars if possible, so it works in browsers with Javascript disabled.

Any idea how this could be done?


#2

If you are talking about all posts in the blog you could use the {{#get}} helper along with an order parameter of published_at asc and a limit of 1 to get the first published post and an order of published_at_desc to get the last post.

If you are talking about particular tags then you could add that into the filter parameter.

Example for the entire blog:

{{#get "posts" limit="1" order="published_at asc"}}
    {{#foreach posts}}
        First post url: {{url}}
    {{/foreach}}
{{/get}}

{{#get "posts" limit="1" order="published_at desc"}}
    {{#foreach posts}}
        Last post url: {{url}}
    {{/foreach}}
{{/get}}

#3

Thanks @Kevin. order="published_at… is exactly what I was looking for — I guess I wasn’t quite firing on all cylinders :innocent:.