0

在 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>
4

1 回答 1

0

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()会为每个页面迭代标签,但对于拥有数百个页面的网站来说,它的运行速度足够快。这是相关的问题。

于 2021-05-21T11:14:00.353 回答