摘要:添加置顶功能。

home.html中使用liquid语言加入if判断,添加post类型layouts的页头属性top。 遍历所有post,当值为true时,使用强调体<strong>加入“置顶”样式,然后正常输出到页面上。 再次遍历,当值不是true,使用默认方式输出即可。

该网站中置顶功能被实现在Minima主题自定义指南

home.html中修改过的内容,其代码如下:

{%- for post in site.posts -%}
    <li>
        {%-if post.top == true -%}
          <strong class="post-meta">置顶<br /></strong>

          {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
          <span class="post-meta">{{ post.date | date: date_format }}</span>
          <h3>
            <a class="post-link" href="{{ post.url | relative_url }}">
              {{ post.title | escape }}
            </a>
          </h3>
          {%- if site.show_excerpts -%}
            {{ post.excerpt }}
          {%- endif -%}
        {%- endif -%}
      </li>
      {%- endfor -%}

      {%- for post in site.posts -%}
      <li>
        {%-if post.top != true -%}
          {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
          <span class="post-meta">{{ post.date | date: date_format }}</span>
          <h3>
            <a class="post-link" href="{{ post.url | relative_url }}">
              {{ post.title | escape }}
            </a>
          </h3>
          {%- if site.show_excerpts -%}
            {{ post.excerpt }}
          {%- endif -%}
        {%- endif -%}
    </li>
{%- endfor -%}

11.24日更新:修改了置顶逻辑实现

  {%- if site.posts.size > 0 -%}
    <h2 class="post-list-heading">{{ page.list_title | default: "博文" }}</h2>
    <ul class="post-list">
      {%- assign top_posts = site.posts | where: "top", true -%}
      {%- assign regular_posts = site.posts | where_exp: "post", "post.top != true" -%}

      <!-- 遍历置顶文章 -->
      {%- for post in top_posts -%}
      <li>
        <strong class="post-meta">置顶<br /></strong>
        {%- assign date_format = site.minima.date_format | default: "%Y-%m-%d" -%}
        <span class="post-meta">{{ post.date | date: date_format }}</span>
        <h3>
          <a class="post-link" href="{{ post.url | relative_url }}">
            {{ post.title | escape }}
          </a>
        </h3>
        {%- if site.show_excerpts -%}
          {{ post.excerpt }}
        {%- endif -%}
      </li>
      {%- endfor -%}

      <!-- 遍历非置顶文章 -->
      {%- for post in regular_posts -%}
      <li>
        {%- assign date_format = site.minima.date_format | default: "%Y-%m-%d" -%}
        <span class="post-meta">{{ post.date | date: date_format }}</span>
        <h3>
          <a class="post-link" href="{{ post.url | relative_url }}">
            {{ post.title | escape }}
          </a>
        </h3>
        {%- if site.show_excerpts -%}
          {{ post.excerpt }}
        {%- endif -%}
      </li>
      {%- endfor -%}
    </ul>