Jekyll - 如果页面是帖子,更改布局?

Zaz*_*Zaz 27 liquid jekyll

在Jekyll布局中,有没有办法检测页面是普通页面还是帖子?我想显示帖子标题,但不显示页面标题.像这样:

{% if page.is_post? %}
    <h2>{{ page.title }}</h2>
{% endif %}
{{ content }}
Run Code Online (Sandbox Code Playgroud)

kav*_*yao 27

从Jekyll 2.0开始,您可以使用Front Matter Defaults:

defaults:
  -
    scope:
      path: ""      # empty string for all files
      type: posts   # limit to posts
    values:
      is_post: true # automatically set is_post=true for all posts
Run Code Online (Sandbox Code Playgroud)

然后你可以{{ page.is_post }}用来检查页面是否发布.

不知道为什么Jekyll没有page.type默认设置.


daf*_*afi 12

在前面宣布一个帖子布局是不够的?如果您的帖子使用post布局,您确定该页面是一个帖子,您不需要添加额外的逻辑

---
layout: post
---
Run Code Online (Sandbox Code Playgroud)

BTW快速而脏(非常脏)的方式来确定页面类型包括检查页面路径,一般帖子都在_posts目录下,所以你可以查看它

{% if page.path contains '_posts' %}
This page is a post
{% else %}
This page is a normal page
{% endif %}
Run Code Online (Sandbox Code Playgroud)


Zaz*_*Zaz 7

这是我解决问题的方法:

  1. _layouts/post→ 创建符号链接_layouts/main
  2. 将帖子的布局更改为post:

    ---
    layout: post
    ---
    
    Run Code Online (Sandbox Code Playgroud)
  3. _layouts/main像这样添加if语句:

    {% if page.layout == 'post' %}
        <h2>{{ page.title }}</h2>
    {% endif %}
    
    Run Code Online (Sandbox Code Playgroud)


解决这个问题的一个更好的方法可能是使用包含,并有两个单独的布局,如@dafi说.


kim*_*udi 5

确定其页面或帖子是否使用的最简单,最直接的方法page.id.

{% if page.id %}
    This is a post
{% endif %}
Run Code Online (Sandbox Code Playgroud)

我个人在我的布局页面中使用这个方法来确定它是一个页面还是帖子,所以我只能在帖子中显示上一个/下一个帖子的链接.

_layouts/default.html中

<!DOCTYPE html>
<html lang="en">

{% include head.html %}

<body>

{% include header.html %}

{{ content }}

<!-- If this is a post, show previous/next post links -->
{% if page.id %}

{% if page.previous.url %}
<a href="{{page.previous.url}}">{{page.previous.title}}</a>
{% endif %}

{% if page.next.url %}
<a class="button is-link ellipsis" title="{{page.previous.title}}" href="{{page.next.url}}">{{page.next.title}}</a>
{% endif %}

{% endif %}

{% include footer.html %}

</body>
</html>
Run Code Online (Sandbox Code Playgroud)