Ghost: newest post with specific tag on the front page
Asked Answered
F

5

6

I am developing a Ghost template for my blog. And I want to see on the front page only one newest post from posts with specific tag (eg "news").

Is it possible to filter posts by tag in foreach loop?

Any other solution? (the first thing that comes to mind is some trick with custom template tag-news.hbs and URL rewriting)

Freemasonry answered 8/12, 2014 at 11:27 Comment(1)
+1 - i want to get just 2 posts on first page (home.hbs, one featured and the latest), but more posts on subsequent index.hbs pagesPrentiss
M
12

You can definitely filter posts by tag using the {{has}} helper:

{{#foreach posts}}
  {{#has tag="news"}}
      {{> post}}
  {{/has}}
{{/foreach}}

You could add this code to a home.hbs file and it would only be used on your homepage.

I'm not sure of the best way to limit it to one post if you want other list pages to have more than one post though. You may have to write a custom helper.

You do have access to an @index variable, but if the first post with 'news' is the third post, @index will be 2 because it increments with the outer foreach loop.

Soon you should be able to use the api: https://github.com/TryGhost/Ghost/wiki/%5BWIP%5D-API-Documentation

Manmade answered 9/12, 2014 at 5:49 Comment(1)
Thanks for your answer. Sad, but seems like this API will not have flexible requests features like /ghost/api/v0.1/posts/tag/:id etc. I think best workaround at this time is to play with nginx url rewriting.Freemasonry
F
5

I've created a little hack for Ghost. It adds {{by_tag}} helper to the theme templates

  1. Create helpers.js in ghost directory with code from this gist
  2. Add first line to your config.js: require('./helpers')();
  3. Restart ghost

{{#by_tag}} Helper

Select posts by tag. Optional limit parameter.

Example:

 {{#by_tag 'dev'}}
     {{#foreach posts}}
         {{title}}
         {{content}}
     {{/foreach}}
 {{/by_tag}}

 {{#by_tag 'music' limit=3}}
    {{#foreach posts}}
         {{title}}
         {{content}}
     {{/foreach}}
 {{/by_tag}}

{{#node_env}} Helper

Example:

{{#node_env production}}
     ...production only 
{{/node_env}}
Freemasonry answered 26/1, 2015 at 9:20 Comment(0)
B
5

After having a read of the lengthy discussion GitHub Ghost Issue: Query (get) helper #4439 recently closed, great news - helpers and filters are being added to Public API v1!

The {{#get}} helper #5619 has just been merged to master (still unstable), so the solution:

{{#get "posts" featured="true" as |featured|}}
  {{#foreach featured}}
    ...
  {{/foreach}}
 {{/get}}
Boeke answered 16/10, 2015 at 10:44 Comment(1)
it doesn't work for me, I still get posts without being featuredValladolid
V
2

Just to add a little useful info, if you want to add featured posts, this is how:

{{#get "posts" filter="featured:true" as |featured| }}
  <ol>
  {{#foreach featured}}
    <li><a href="{{url}}">{{title}}</a></li>
  {{/foreach}}
  </ol>
{{/get}}

To get a rough idea of what more you can do, visit the official GitHub

Hope it helps

Valladolid answered 17/8, 2018 at 23:21 Comment(0)
O
0

In index.hbs:

    <div class="post-feed">
      {{#foreach posts}}
          {{^has tag="videos"}}
            {{! this block will not show posts tagged videos }}
            {{> "post-card"}}
          {{/has}}
      {{/foreach}}
    </div>

In tag-videos.hbs:

    <div class="post-feed">
        {{#foreach posts}}
            {{#has tag="videos"}}
              {{! this block will show posts tagged videos }}
              {{> "post-card"}}
            {{/has}}
        {{/foreach}}
    </div>

Hope this helps!

Operculum answered 5/3, 2018 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.