Docs: Filtering Posts with categories, tags, or other variables (#6399)

Merge pull request 6399
This commit is contained in:
Kenton Hansen 2017-10-02 08:47:05 -05:00 committed by jekyllbot
parent b11ad8ea77
commit 77bb9267ac
1 changed files with 46 additions and 1 deletions

View File

@ -155,6 +155,51 @@ you wish to access the currently-rendering page/posts's variables (the
variables of the post/page that has the `for` loop in it), use the `page`
variable instead.
## Displaying post categories or tags
Hey, that's pretty neat, but what about showing just some of your posts that are
related to each other? For that you can use any of the [variables definable in
Front Matter](https://jekyllrb.com/docs/frontmatter/). In the "typical post"
section you can see how to define categories. Simply add the categories to your
Front Matter as a [yaml
list](https://en.wikipedia.org/wiki/YAML#Basic_components).
Now that your posts have a category or multiple categories, you can make a page
or a template displaying just the posts in those categories you specify. Here's
a basic example of how to create a list of posts from a specific category.
First, in the `_layouts` directory create a new file called `category.html` - in
that file put (at least) the following:
```html
---
layout: page
---
{% for post in site.categories[page.category] %}
<a href="{{ post.url | prepend: site.baseurl }}">
{{ post.title }}
</a>
<hr>
{% endfor %}
```
Next, in the root directory of your Jekyll install, create a new directory
called `category` and then create a file for each category you want to list. For
example, if you have a category `blog` then create a file in the new directory
called `blog.html` with at least
```text
---
layout: category
title: Blog
category: blog
---
```
In this case, the listing pages will be accessible at `{baseurl}/category/blog.html`
While this example is done with categories, you can easily extend your lists to
filter by tags or any other variable created with extensions.
## Post excerpts
Each post automatically takes the first block of text, from the beginning of