How to Show Featured Posts on Homepage in Your Ghost Blog
Sometimes, on your Ghost blog, you want to put your featured posts above the rest on homepage, on a custom section. In order to do that you need to get a list with all your featured posts.
To do that we need to use the get helper.
Add the following code on index.hbs or home.hbs, in your Ghost theme.
{{^is "paged"}} {{#get "posts" filter="featured:true" limit="all"}} {{#if posts}} {{#foreach posts}} <h2><a href="{{url}}" title="{{title}}">{{title}}</a></h2> {{/foreach}} {{/if}} {{/get}} {{/is}}
Let's go through this code and explain what is going on.
- We use the is helper with negation (^). This piece of code will run the code only on the pages that are not paged. So the code will be visible on homepage but it will not be visible on /page/2/ of your list.
- We use the get helper to fetch the featured posts only. For that we use the filter parameter and set it to featured:true. The limit is set to all but you can change that in a specific number. Like limit="4". If you remove this limit it will show the latest 15 featured posts, 15 is the default number set on Ghost.
- We do a simple if check to make sure that we have some posts fetched. If we don't have, we don't show the code.
- Then we use the foreach helper to loop through all the posts that we fetched.
As a live example we have Joelma Ghost Theme that shows the featured posts in a slider on homepage.
For any questions feel free to ask any questions below.