Ghost: newest post with specific tag on the front page

我是研究僧i 提交于 2019-11-30 05:34:21

问题


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)


回答1:


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




回答2:


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}}



回答3:


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}}



回答4:


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!




回答5:


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



来源:https://stackoverflow.com/questions/27356986/ghost-newest-post-with-specific-tag-on-the-front-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!