【问题标题】:Jekyll display posts by categoryJekyll 按类别显示帖子
【发布时间】:2014-01-19 07:19:59
【问题描述】:

为此挠头 - 非常感谢您的帮助。

我想显示我所有 Jekyll 帖子的列表,按类别组织。我知道第 3 行不正确,但我不知道它应该是什么。有任何想法吗?谢谢!

{% for category in site.categories %}
    <h3>{{ category | first }}</h3>
    {% for post in page.categories.category %}
      {{ post.title }}<br>
    {% endfor %}            
{% endfor %}

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    知道了!在列出单个帖子之前需要一个中间帖子循环

    <ul>
    {% for category in site.categories %}
      <li><a name="{{ category | first }}">{{ category | first }}</a>
        <ul>
        {% for post in category.last %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
        </ul>
      </li>
    {% endfor %}
    </ul>
    

    【讨论】:

    • 我在本地进行了测试,发现第一个 {{ posts }} 是类别名称,在 html 中将是一个空行,所以我在前面添加 {% if post.url %} @ 987654323@ 删除类别行
    • 嘿,谢谢,效果很好!不过我有一个问题(抱歉,完全是 jekyll noob),可能有助于改进你的答案:你知道(如果可能的话)如何将此代码放在 /category/ 中的页面中,读取 从网址并相应地拉出帖子列表? (编辑)我的问题在这里重复:stackoverflow.com/questions/25958652/…
    【解决方案2】:

    fyi,如果有人只想列出一个类别中的帖子,这是可行的(与上面的示例不同,因为类别返回帖子列表...

    <p>Posts in category "basic" are:</p>
    
    <ul>
      {% for post in site.categories.basic %}
        {% if post.url %}
            <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endif %}
      {% endfor %}
    </ul>
    

    【讨论】:

    • 这是一个更好的答案,并且避免了接受的答案所必需的建议黑客攻击。
    • 如何为包含空格的类别名称执行此操作,例如“foo bar”?
    【解决方案3】:

    现在有一个官方插件可用。 jekyll-archives

    为了使用它,

    jekyll-archives 添加到您的Gemfile_config.yml 文件中。

    根据您的需要添加类似于以下内容的配置。

    jekyll-archives:
      enabled: all
      layouts:
        year: archive/year
        month: archive/month
        day: archive/day
        tag: archive/tag
        category: archive/category
      permalinks:
        year: '/:year/'
        month: '/:year/:month/'
        day: '/:year/:month/:day/'
        tag: '/tags/:name/'
        category: '/category/:name/'
    

    layouts 可以根据archive type 使用以下页面属性。

    • page.type - (以下任意一个。yearmonthdaytagcategory
    • page.title - (仅适用于类型标签和类别。Nil 否则。)
    • page.date -(取决于page.type,您应该解析出日期和月份字段)
    • page.posts -(此存档的帖子列表)

    这是基于年份的存档示例布局

    <h1>Archive of posts from {{ page.date | date: "%Y" }}</h1>
    <ul class="posts">
    {% for post in page.posts %}
      <li>
        <span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
        <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
      </li>
    {% endfor %}
    </ul>
    

    【讨论】:

      【解决方案4】:
      <h5>Categories</h5>
      {% for category in site.categories %}
          {% assign cat = category[0] %}
          <h6><a href="#">{{ cat }}</a></h6>
          {% for post in site.categories[cat] %}
              <a href="{{ post.url }}">{{ post.title }}</a> <small>{{ post.date }}</small>
          {% endfor %}
      {% endfor %}
      

      【讨论】:

        【解决方案5】:

        我不记得确切的语法,但类似以下代码的代码应该检索类别名称,以便您检索每个类别的帖子...

        {% for category in site.categories %}
        
           {% assign cat_name = category[0] %}
        
          {% for post in site.categories.cat_name %}
        
               ...
        
          {% endfor%}
        
         {% endfor %}
        

        【讨论】:

        • 不工作。读取{% for post in site.categories[cat_name] %}
        【解决方案6】:

        这是一个使用排序的答案(有用!):

        {% comment %}
        #
        #  Change date order by adding '| reversed'
        #  To sort by title or other variables use {% assign sorted_posts = category[1] | sort: 'title' %}
        #
        {% endcomment %}
        {% assign sorted_cats = site.categories | sort %}
        {% for category in sorted_cats %}
        {% assign sorted_posts = category[1] | reversed %}
        <h2 id="{{category[0] | uri_escape | downcase }}">{{category[0] | capitalize}}</H2>
        <ul>
          {% for post in sorted_posts %}
            <li><a href="{{ site.url }}{{ site.baseurl }}{{  post.url }}">{{  post.title }}</a></li>
          {% endfor %}
        </ul>
        {% endfor %}
        

        这不是我的,取自here

        【讨论】:

          猜你喜欢
          • 2018-07-09
          • 1970-01-01
          • 2019-03-26
          • 1970-01-01
          • 2015-03-24
          • 1970-01-01
          • 1970-01-01
          • 2014-12-12
          • 1970-01-01
          相关资源
          最近更新 更多