网站内容添加(改善本页StepByStep教程让我们来试着充实作者,只是内容不必按日期分组)

优采云 发布时间: 2022-04-19 15:40

  网站内容添加(改善本页StepByStep教程让我们来试着充实作者,只是内容不必按日期分组)

  改进此页面

  StepByStep 教程9. 合集

  让我们尝试充实作者,以便每个作者都有自己的页面,其中收录他们的简历和帖子。

  为此,您将使用集合。集合类似于博客文章,只是内容不必按日期分组。

  配置

  你需要告诉 Jekyll 一些相关信息来建立一个集合。默认情况下,Jekyll 将配置放在一个名为 _config.yml 的文件中。

  在根目录下新建一个_config.yml文件,内容如下:

  collections:

authors:

  要(重新)加载配置,需要重新启动 jekyll 服务器。在终端中按 Ctrl+C 停止服务器,然后运行 ​​jekyll serve 重新启动它。

  添加作者

  文档(集合中的项目)位于 网站 根目录下名为 _*采集_name* 的文件夹中。这里称为_authors。

  为每个作者创建一个新文档:

  _authors/jill.md:

  ---

short_name: jill

name: Jill Smith

position: Chief Editor

---

Jill 是一个狂热的水果种植者,他住在法国南部。

  _authors/ted.md:

  ---

short_name: ted

name: Ted Doe

position: Writer

---

Ted 吃了一辈子水果。

  人员页面

  让我们添加一个页面,列出该网站的所有作者。 Jekyll 使该集合可用于 site.authors 变量表示。

  新建staff.html,遍历site.authors输出所有staff:

  ---

layout: default

title: 人员

---

人员

{% for author in site.authors %}

{{ author.name }}

{{ author.position }}

<p>{{ author.content | markdownify }}

{% endfor %}

</p>

  由于内容是markdown文件,需要在运行时通过markdownify过滤器。这是在布局中使用 {{ content }} 输出时自动完成的。

  您还需要一种从主导航导航到此页面的方法。打开 _data/navigation.yml 并为人员页面添加一个条目:

  - name: 主页

link: /

- name: 关于

link: /about.html

- name: 博客

link: /blog.html

- name: 人员

link: /staff.html

  输出一个单独的页面

  默认情况下,集合不输出文档页面。在这种情况下,我们希望每个作者都有自己的页面,所以让我们调整集合的配置。

  打开 _config.yml 并将 output: true 添加到作者集合的配置中:

  collections:

authors:

output: true

  您可以使用 author.url 链接到这些输出页面。添加staff.html页面的链接:

  ---

layout: default

---

人员

{% for author in site.authors %}

{{ author.name }}</a>

{{ author.position }}

<p>{{ author.content | markdownify }}

{% endfor %}

</p>

  与博客文章一样,您需要为作者页面创建布局。

  创建一个新的_layouts/author.html,内容如下:

  ---

layout: default

---

{{ page.name }}

{{ page.position }}

{{ content }}

  默认前台

  现在您需要配置作者文档以使用作者布局。您可以像我们之前所做的那样在前面执行此操作,但它会变得重复。

  我们真正想要的是所有帖子都自动具有帖子布局,作者具有作者布局,而其他所有内容都具有默认布局。

  您可以通过使用 _config.yml 中的默认前端来做到这一点。你可以设置默认值的范围,然后设置你想要的默认前置内容。

  将布局默认值添加到您的 _config.yml

  collections:

authors:

output: true

defaults:

- scope:

path: ""

type: "authors"

values:

layout: "author"

- scope:

path: ""

type: "posts"

values:

layout: "post"

- scope:

path: ""

values:

layout: "default"

  您现在可以从所有页面和帖子的前端删除布局。请注意,无论何时更新 _config.yml ,都需要重新启动 Jekyll 才能使更改生效。

  列出作者的帖子

  让我们在每个作者的页面上列出每个作者的帖子。为此,您需要将帖子的作者与作者的 short_name 匹配。您可以通过这种方式按作者过滤帖子。

  遍历 _layouts/author.html 中的过滤列表以输出作者的帖子:

  ---

layout: default

---

{{ page.name }}

{{ page.position }}

{{ content }}

帖子

{% assign filtered_posts = site.posts | where: 'author', page.short_name %}

{% for post in filtered_posts %}

{{ post.title }}</a>

{% endfor %}

  链接到作者页面

  这些帖子引用了作者,所以让我们将它们链接到作者页面。您可以在 _layouts/post.html 中使用类似的过滤技术来实现这一点。

  ---

layout: default

---

{{ page.title }}

<p>

{{ page.date | date_to_string }}

{% assign author = site.authors | where: 'short_name', page.author | first %}

{% if author %}

- {{ author.name }}</a>

{% endif %}

{{ content }}

</p>

  打开 :4000 并查看人员页面和帖子上的作者链接,以检查所有内容是否正确链接在一起。

  在本教程的下一步也是最后一步中,我们将为站点添加润色并准备部署。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线