【问题标题】:Single Index Blog Post for Multiple Series Posts in JekyllJekyll 中多个系列文章的单一索引博客文章
【发布时间】:2018-09-10 15:15:48
【问题描述】:

我有几篇博文属于一篇综合博文。例如,我有几篇关于 SQL Zoo 教程的帖子,但我希望能够将它们全部链接到一篇“伞形”帖子,这样我博客的索引页面上就只有一篇 SQL Zoo 帖子。我从:https://codeasashu.github.io/implement-series-post-jekyll-part-2/ 得到了这个想法,并尝试按照说明进行操作,但现在我的系列文章没有出现在我的索引页面上。我的 _includes 文件夹中名为 post-series.html 的文件中有以下代码:

{% assign seriesarray = '|' | split : '|' %}
{% assign seriestitle = '' %}
{% assign serieslabel = '' %}
{% assign sortedposts = (site.posts | sort: 'date') %}
{% for post in sortedposts %}
    {% if post.series and page.series_slug != nil and post.series == page.series_slug %}

        {% capture postitem %}    <li> <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a> </li> {% endcapture %}
        {% assign seriesarray = seriesarray | push: postitem %}
        {% assign seriestitle = 'Posts in this series' %}
        {% assign serieslabel = 'Series Post' %}

    {% elsif post.series != nil and page.series != nil and page.series == post.series %}

        {% assign pageurl = page.url | split:'/' | last %}
        {% assign posturl = post.url | split:'/' | last %}
        {% if pageurl != posturl %}
        {% capture postitem %}    <li> <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a> </li> {% endcapture %}
        {% else %}
        {% capture postitem %}    <li> {{ post.title }} </li> {% endcapture %}
        {% endif %}
        {% assign seriesarray = seriesarray | push: postitem %}

    {% endif %}

    {% if post.series_slug != nil and page.series != nil and page.series == post.series_slug %}
        {% capture series_title %} <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>  {% endcapture %}
        {% assign seriestitle = 'This posts is part of series - ' | append: series_title %}
        {% assign serieslabel = 'This posts is part of series - ' | append: series_title %}
    {% endif %}

{% endfor %}

{% capture serieslayout %}
    {% if seriesarray.size > 0 %}
    <hr />
    <div class="panel">
        <div class="panel-body">
        <h4> {{ seriestitle }} </h4>
        <ul id="post-series-list">
    {% endif %}
    {% for post in seriesarray %} {{ post }} {% endfor %}
    {% if seriesarray.size > 0 %} </ul> </div> </div> {% endif %}
{% endcapture %}

以及我目录根目录下的 index.html 文件中的以下代码:

---
layout: index
---

<div id="home">
  <h1>{{ site.title }}</h1>
  <hr />

<ol class="posts">
  {% for post in paginator.posts %}
  {% assign seriesPost = nil %}
  {% if post.series == nil %}
  {% if post.series_slug != nil %} {% assign seriesPost = '(Series)' %} {% endif %}
    <li class="post-listing">
      <img class="post__image" src="/static/img/{{ post.cover_image}}" alt="{{ post.cover_alt }}" />
      <div class="post__text">
        <a class="post__title" href="{{ post.url }}">{{ post.title }}</a><br>
        <span>
          {{ post.date | date_to_string }} &bull;
          {% assign words = post.content | number_of_words %}
          {% if words < 360 %}
            1 min read
          {% else %}
            {{ words | divided_by:180 }} min read
          {% endif %}
        </span>
        {{ post.excerpt }}
      </div>
    </li>
    {% endif %}
    {% endfor %}
  </ol>

  <!-- <div class="sidebar-right sidebar"></div> -->

  <!-- <ul>
    {% for post in paginator.posts %}
      <li>
        <a href="{{ post.url }}">{{ post.title }}</a>
        {{ post.excerpt }}
      </li>
    {% endfor %}
  </ul> -->

  <!-- Pagination links -->
  {% if paginator.total_pages > 1 %}
    <ul class="pagination pagination-sm">
      {% if paginator.previous_page %}
        <li><a href="{{ paginator.previous_page_path | prepend: site.baseurl | replace: '//', '/' }}">&laquo;</a></li>
      {% else %}
        <li class="disabled"><span aria-hidden="true">&laquo;</span></li>
      {% endif %}

      <li><a href="/">First</a></li>

      {% for page in (1..paginator.total_pages) %}
        {% if page == paginator.page %}
          <li class="active"><a>{{ page }}<span class="sr-only">(current)</span></a></li>
        {% elsif page == 1 %}
          <li><a href="/">{{ page }}</a></li>
        {% else %}
          <li><a href="{{ site.paginate_path | prepend: site.baseurl | replace: '//', '/' | replace: ':num', page }}">{{ page }}</a></li>
        {% endif %}
      {% endfor %}

      <li><a href="/page/{{ paginator.total_pages }}/">Last</a></li>

      {% if paginator.next_page %}
        <li><a href="{{ paginator.next_page_path | prepend: site.baseurl | replace: '//', '/' }}">&raquo;</a></li>
      {% else %}
        <li class="disabled"><span>&raquo;</span></li>
      {% endif %}
    </ul>
  {% endif %}
</div><!-- end #home -->

我的完整回购可以在这里找到:https://github.com/thedatasleuth/thedatasleuth.github.io

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    在您的 index.html 中,{% if post.series == nil %} 仅显示包含要打印的 series: someserie 前端变量的帖子。

    对于第二个问题(请注意,在 SO 上,您应该一次问一个问题):

    1. 伞柱前面总是有series_slug: "My serie slug" 很重要。
    2. Serie 的帖子前面总是有series: "My serie slug" 重要的是,这必须严格等于伞形页面series_slug。 (例如:您有一篇带有 published: falseseries: "SQL Zoology" 的帖子,如果您发布它,这些帖子将不会出现在 SQL Zoo 系列中。)

    _layouts/post.html 中删除 {% include series.html %}

    _includes/post-series.html 中将所有代码替换为以下内容:

    {% comment %} #### On an umbrella page {% endcomment %}
    {% if page.series_slug != nil %}
      {% assign key = page.series_slug %}
      {% assign title = page.title %}
      {% assign url = page.url %}
      {% assign sentence = "All posts in this serie :" %}
    {% endif %}
    
    {% comment %} #### On a serie page {% endcomment %}
    {% if page.series != nil %}
      {% assign key = page.series %}
      {% assign umbrella_page = site.posts | where: 'series_slug', key | first %}
      {% assign title = umbrella_page.title %}
      {% assign url = umbrella_page.url %}
      {% assign series_posts = site.posts | where: "series", key %}
      {% for post in series_posts %}
        {% if post.url == page.url %}
          {% assign idx = forloop.index %}
        {% endif %}
      {% endfor %}
      {% capture sentence %}
      This article is <strong>Part {{ idx }}</strong> in a <strong>{{ series_posts.size }}-Part</strong> in <a href="{{ site.baseurl }}{{ url }}">{{ title }} serie</a>
      {% endcapture %}
    {% endif %}
    
    {% if page.series_slug != nil or page.series != nil %}
      {% assign series_posts = site.posts | where: "series", key %}
      <hr />
      <div class="panel">
        <div class="panel-body">
          {% if page.series_slug != nil %}
            {% assign key = page.series_slug %}
            {% assign title = page.title %}
            {% assign url = page.url %}
          {% endif %}
    
          <h4>{{ sentence }}</h4>
          <ul id="post-series-list">
          {% for post in series_posts %}
            <li>
            {% if page.url == post.url %}
              This post : {{ post.title }} - part {{ forloop.index }}
            {% else %}
              <a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }} - part {{ forloop.index }}</a>
            {% endif %}
            </li>
          {% endfor %}
          </ul>
        </div>
      </div>
    {% endif %}
    

    【讨论】:

    • 好的,我已将 YAML 系列和 series_slug 重新添加到系列中。现在,我的索引页面上显示了伞帖。我最终希望我的伞形页面看起来像这样:codeasashu.github.io/implement-series-post-jekyll 其中包含指向相关子帖子的超链接,但现在,我只是手动超链接了我的子帖子。
    • 非常感谢 - 这完全符合我的设想。
    【解决方案2】:

    你面临的问题

    您忘记添加 seriesseries_slug YML 变量。这就是为什么它不适用于您的情况。您似乎没有完全理解代码的工作原理,可能是由于解决方案的复杂性。因此,我添加了另一个(更简单的)解决方案,它可能同样适合您的需求。


    更好/更简单的解决方案

    只需将一个名为 group: groupname 的 YML 变量添加到您想要在组中的每个帖子。在分页期间不要跳过任何帖子。接下来,使用下面的代码在每个帖子的页脚(在您的页面/帖子布局中)列出具有相同组的帖子。最后,在系列中的帖子名称中添加“第 1 部分”、“第 2 部分”等。

    {% if post.group != nil %}
      <ul>
      {% for post in site.posts %}
        {% if post.group == page.group %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endif %}
      {% endfor %}
      </ul>
    {% endif %}
    

    PS。如果你真的想跳过分页中的一些帖子,我会创建一个通用的解决方案。在 Front Matter (YML) 中添加一个 skip_pagination 布尔值,并在分页循环中查看它是否为真。这使您可以跳过分页器中的任何帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-25
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多