How to Count All Posts in Ghost
Sometimes you might want to do a custom feature inside your Ghost theme and you need the total number of posts in your blog.
You can do this by using get
helper. Let's fetch all posts in the blog by setting limit="all"
. If you don't set a limit, the default one, 15, will be used. To show the total number of posts fetched use {{posts.length}}
. Here is the full code:
{{#get "posts" limit="all"}} {{posts.length}} {{/get}}
You can also use {{pagination.total}}
to display the total number of posts. This code will only work on pages that accept pagination: index.hbs
, author.hbs
and tag.hbs
.
You might want to count all posts with JavaScript:
$.get(ghost.url.api('posts', {limit: 'all'})).done(function (data){ console.log('posts', data.posts.length); });
That's all.