【问题标题】:JBake: list all posts with a particular tag (tagged_posts)JBake:列出所有带有特定标签的帖子(tagged_posts)
【发布时间】:2021-05-21 08:18:31
【问题描述】:

在 freemarker 中,我如何遍历所有带有特定标签的博客文章,例如标签“算法”?

<#assign relatedBlogs = ...>
<#if relatedBlogs?size &gt; 0>
    <h2>Related blog posts</h2>
    <ul>
        <#list relatedBlogs as blog>
            <li>
                <a href="${content.rootpath}${blog.uri}">${blog.title}</a>
            </li>
        </#list>
    </ul>
</#if>

这对我不起作用:

<#assign relatedBlogs = tagged_posts["algorithms"]>

这也不起作用:

<#assign relatedBlogs = tags["algorithms"].tagged_posts>

【问题讨论】:

    标签: freemarker jbake


    【解决方案1】:

    tags 是一个序列,要使用tags[...],您需要一个哈希。 我最终让它通过?filter()?first工作:

    <#assign relatedTags = tags?filter(tag -> tag.name == "algorithms")>
    <#if relatedTags?size &gt; 0>
        <#assign relatedTag = relatedTags?first>
        <h2>Algorithms related blog posts</h2>
        <ul>
            <#list relatedTag.tagged_posts as blog>
                <li>
                    <a href="${content.rootpath}${blog.uri}">${blog.title}</a>
                </li>
            </#list>
        </ul>
    </#if>
    

    这不会很好地扩展,因为filter() 会迭代每个页面的标签,但对于拥有数百个页面的网站来说,它的运行速度足够快。 Here's the related issue.

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多