【问题标题】:Github Pages: adding an if condition in index.htmlGithub Pages:在 index.html 中添加 if 条件
【发布时间】:2021-12-16 00:06:15
【问题描述】:

我在 Github 页面上使用 Jekyll Now 创建了一个博客

默认的 Index.html 是这样的

---
layout: default
---

<div class="posts">
  {% for post in site.posts %}
        <article class="post">

          <h2><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a></h2>

          <div class="entry">
            {{ post.excerpt }}
          </div>

          <a href="{{ site.baseurl }}{{ post.url }}" class="read-more">Read More</a>
        </article>
  {% endfor %}
</div>

这将创建一个登录页面,您在 _posts 目录中创建的所有帖子的标题都将显示并带有链接。

查看{% for ... %} & {% endfor %} & 最终的静态 HTML,似乎在构建页面时,for 标签实际上是迭代的,最终的 HTML 包含一个实际标题列表。

我想更改此设置,以便不会列出所有帖子。我不想列出任何标题包含字符串“BEINGWRITTEN”的帖子

我尝试过类似的东西

{% for post in site.posts %}
    {% if (post.title.indexOf('BEINGWRITTEN') == -1) %}
        <article class="post">
...
        </article>
    {% endif %}
{% endfor %}

还尝试使用包含而不是 indexOf,这也不起作用。在这两种情况下,我都没有在着陆页上看到任何链接的帖子。

我该怎么做?

【问题讨论】:

    标签: if-statement github conditional-statements github-pages blogs


    【解决方案1】:

    我通过在我不想包含的页面的前面添加一个类别来做到这一点。

    即 类别:不显示

    然后把index.html改成

       {% for post in site.posts %}
    
          {% unless post.category == "noshow" %}
    
              .....
    
          {%endunless}
    
       {%endfor}
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 2022-12-30
      • 1970-01-01
      • 2018-01-03
      • 2020-09-20
      • 2012-09-22
      • 2016-12-07
      • 2021-09-19
      • 2017-05-28
      相关资源
      最近更新 更多