簡體   English   中英

Ghost:首頁上帶有特定標簽的最新帖子

[英]Ghost: newest post with specific tag on the front page

我正在為我的博客開發一個Ghost模板。 我想在首頁上看到來自具有特定標簽的帖子的最新帖子(例如“新聞”)。

是否可以在foreach循環中按標簽過濾帖子?

還有其他方法嗎? (首先想到的是自定義模板tag-news.hbs和URL重寫的一些技巧)

您絕對可以使用{{has}}幫助器按標記過濾帖子:

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

您可以將此代碼添加到home.hbs文件中,它只會在您的主頁上使用。

如果您希望其他列表頁面有多個帖子,我不確定將其限制為一個帖子的最佳方法。 您可能必須編寫自定義幫助程序。

您可以訪問@index變量,但如果帶有'news'的第一個帖子是第三個帖子,則@index將為2因為它隨外部foreach循環遞增。

很快你就可以使用api: https//github.com/TryGhost/Ghost/wiki/%5BWIP%5D-API-Documentation

在閱讀了冗長的討論后, GitHub Ghost問題:查詢(獲取)幫助#4439最近關閉,好消息 - 幫助程序和過濾器正被添加到Public API v1

{{#get}}幫助程序#5619剛剛合並為master(仍然不穩定),因此解決方案:

{{#get "posts" featured="true" as |featured|}}
  {{#foreach featured}}
    ...
  {{/foreach}}
 {{/get}}

我為Ghost創建了一個小黑客。 它將{{by_tag}}幫助器添加到主題模板中

  1. 使用此gist中的代碼在ghost目錄中創建helpers.js.
  2. 第一行添加到config.js: require('./helpers')();
  3. 重啟幽靈

{{#by_tag}}助手

按標簽選擇帖子。 可選limit參數。

例:

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

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

{{#node_env}}助手

例:

{{#node_env production}}
     ...production only 
{{/node_env}}

index.hbs

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

tag-videos.hbs

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

希望這可以幫助!

只是添加一些有用的信息,如果你想添加精選帖子,這是如何:

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

要了解您還可以做些什么,請訪問官方GitHub

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM