[英]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}}幫助器添加到主題模板中
require('./helpers')();
按標簽選擇帖子。 可選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 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.