The Ghost Themes Developer Hub

Welcome to the Ghost Themes developer hub. You'll find comprehensive guides and documentation to help you start working with Ghost Themes as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

prev_post & next_post

{{prev_post}} & {{next_post}} Helpers - Fetch the next or previous post from the current post

Usage: {{#prev_post}}{{title}}{{/prev_post}} - {{#next_post}}{{title}}{{/next_post}}

Types
Parameters
Attributes

n/a

  • in (string, "primary_tag" or "author")

Description

When in the scope of a post, you can call the next or previous post helper, which performs a query against the API to fetch the next or previous post in accordance with the chronological order of the blog.

Inside of the opening and closing tags of the {{#next_post}}{{/next_post}} or {{#prev_post}}{{/prev-post}} helper, the normal helpers for outputting posts will work, but will output the details of the post that was fetched from the API, rather than the original post.

{{#post}}
	<h2>{{title}}</h2>
	<div class="{{post_class}}">
		{{content}}
	</div>

	{{#prev_post}}
		<a href="{{url}}">{{title}}</a>
	{{/prev_post}}

	{{#next_post}}
		<a href="{{url}}">{{title}}</a>
	{{/next_post}}
{{/post}}

Like all block helpers, next_post and prev_post support adding an {{else}} block or using ^ instead of # for negation - this means that the {{#next_post}} and {{else}} blocks are reversed if you use {{^next_post}} and {{else}} instead. This can be used to output content if there is no next or previous post available.

{{#next_post}}
	<p><a href="{{url}}">{{title}}</a></p>
{{else}}
	<p>No more posts!</p>
{{/next_post}}

Note: The next and previous post helpers cannot be nested inside each other. This is because they are performing a query, and we currently cannot support nesting queries inside each other.

You can do more than one query on a page though, so you can use both {{#next_post}} and {{#prev_post}} in the same template, or call either of them multiple times.

Primary Tag Support

If you'd like to output the next or previous post within a primary tag, e.g. within a section of posts, rather than within all posts, this can be done with the "in" parameter:

{{#post}}
	<h2>{{title}}</h2>
  <h3>Filed in {{primary.tag.name}}</h3>
	<div class="{{post_class}}">
		{{content}}
	</div>

	{{#prev_post in="primary_tag"}}
		<a href="{{url}}">{{title}}</a>
	{{/prev_post}}

	{{#next_post in="primary_tag"}}
		<a href="{{url}}">{{title}}</a>
	{{/next_post}}
{{/post}}

If the current post does not have a primary tag, the helpers will fall back to outputting the global next or previous post instead. If this is not the behaviour you want, you can change it so that the next and previous posts are only output IF there is a primary tag, by wrapping the code in an {{#if}} block:

{{#post}}
	<h2>{{title}}</h2>
	<div class="{{post_class}}">
		{{content}}
	</div>

	{{#if primary_tag}}
	  {{#prev_post in="primary_tag"}}
		  <a href="{{url}}">{{title}}</a>
	  {{/prev_post}}

	  {{#next_post in="primary_tag"}}
		  <a href="{{url}}">{{title}}</a>
	  {{/next_post}}
  {{/if}}
{{/post}}

Author Support

Similar to the primary tag support, you can link to the previous or next post that was authored by the current post's author:

{{#post}}
	<h2>{{title}}</h2>
	<h3>Written by {{author.name}}</h3>
	<div class="{{post_class}}">
		{{content}}
	</div>

	{{#prev_post in="author"}}
		<a href="{{url}}">{{title}}</a>
	{{/prev_post}}

	{{#next_post in="author"}}
		<a href="{{url}}">{{title}}</a>
	{{/next_post}}
{{/post}}

Currently, the in="" parameter only accepts primary_tag or author as a value.

prev_post & next_post

{{prev_post}} & {{next_post}} Helpers - Fetch the next or previous post from the current post