有没有办法让一个 Jekyll 网站拥有多个博客?我目前想在一个网站中拥有两个博客。
我是这个页面的作者:http://www.garron.me/blog/multi-blog-site-jekyll.html
如果你需要单独的归档页面和每个博客的最新文章,请使用以下代码:
创建一个名为archives-blog-1.html的文件,并填入以下内容:
{% for post in site.posts %}
{% if post.categories contains 'blog1' %}
<div class="post">
<h3 class="title"><a href="{{ post.url }}">{{ post.title }}</a></h3>
<p class="meta">Date: {{ post.date }}</p>
</div>
{% endif %}
{% endfor %}
这将为您提供博客1中的所有帖子列表,您可以为博客2执行相同操作。该页面可以放置在您想要的任何位置。
对于最新的帖子,您可以使用相同的代码,但将其封装在以下内容之间:
{% for post in site.posts limit:5 %}
....
{% endfor %}
这将为您提供最新的5篇文章... 我正在使用这个
{% for post in site.posts limit:5 %}
<div class="post">
<ul>
<li><a href="{{ post.url }}">{{ post.title | truncate:200 }} </a><small>{{ post.date }}</small>
{% if post.summary %}
<p class="entry">{{ post.summary }}</p>
{% endif %}
</li>
</ul>
</div>
{% endfor %}
在我的索引页面中,http://www.garron.me/index.html...在子标题(来自博客)下方,
我没有限制任何类别,因此所有博客的帖子都会出现在那里,您可以使用{% if post.categories contains 'blog1' %}
进行限制。
希望对你有所帮助。我使用了两个独立的Jekyll安装来运行同一域名下的两个博客;如果你的博客将存在于不同的根目录中(我的在/
和/photos/
),那么我建议采用这种方法。我还描述了如何合并两个博客的sitemap.xml文件。
你最好研究一下数据文件功能。你可以把.markdown
文件放在你的source
文件夹中的一个单独的文件夹中,并在发布时链接到它们。这意味着为了发布文章,你需要编写一个数据文件条目,但你可以托管尽可能多的“博客”,每个博客都有自己的文件夹。帖子将自动将它们所在的文件夹作为url。我用这种方法来写我的个人博客和作品集。
或者,你可能想研究一下集合:http://jekyllrb.com/docs/collections/
在@ggarron的回答中,还有一种按类别过滤的简短版本:
{% assign filtered_posts = site.posts | where_exp: "post", "post.categories contains 'blog1'" %}
site.posts
的地方替换为filtered_posts
并保持一致性。例如,我有一个片段来获取列表中的第一篇文章:{% assign first_post = filtered_posts | first %}
这个按预期工作。