【问题标题】:Can I get posts by using an array of tags using Jekyll?我可以使用 Jekyll 使用一组标签来获取帖子吗?
【发布时间】:2021-07-14 01:17:21
【问题描述】:

我正在为我的 Jekyll 网站编写一个流动脚本来检索相关帖子。我想使用用于当前帖子 (post.tags) 的标签数组进行查询,以匹配网站上也有一个 (或多个) 的其他帖子。如:

assign relatedPosts = site.posts | where 'tags', page.tags

但是,在文档中它只提到传入一个字符串,而不是一个字符串数组?理想情况下,我希望使用 where 运算符 (或类似的东西) 来完成此操作,而不是遍历网站上的每个帖子。

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    您是正确的,您不能为 where 运算符提供数组,根据文档,您只能在那里传递一个字符串。

    根据您的要求,您似乎无法避免遍历网站上的每个帖子,因为基本上您想检查所有帖子是否有几个不同的标签。

    一种可能的解决方案是首先遍历您的帖子标签,然后根据所有网站帖子标签检查该标签,如下所示:

    {% assign new_array = "" | split: "" %}
    {% for tag in page.tags %}
        {% assign tag_found = site.posts | where "tags": tag %}
        {% assign new_array = new_array | concat: tag_found %}
    {% endfor %}
    OUTPUT: {{ new_array }}
    

    【讨论】:

    • 谢谢。我得出了同样的结论,遍历所有帖子是不可避免的,流动的语法并没有我们想要的那么全。我最终使用了本文中代码 sn-p 的修改版本:blog.webjeda.com/jekyll-related-posts
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多