Skip to content

Commit fb2360f

Browse files
melissamcewenMelissa Mcewen
authored andcommitted
Update adding-tags-and-categories-to-blog-posts.md
adding instructions on creating a tag component and linking to tags from blog post page
1 parent 239401e commit fb2360f

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

docs/docs/adding-tags-and-categories-to-blog-posts.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const query = graphql`
5151
}
5252
`
5353
```
54-
Using this query you start utilizing your fields in your component. This lists all posts and their tags
54+
Using this query you start utilizing your fields in your component. This example creates a blog front page that lists all posts and their tags
5555
```jsx
5656
const IndexPage = ({ data }) => (
5757
<div>
@@ -176,3 +176,43 @@ const createTagPages = (createPage, edges) => {
176176
});
177177
}
178178
```
179+
180+
## Adding Tags To Your Blog Front Page
181+
The blog front page has a list of the tags, but it doesn't list to the tag pages. To do this, create a tag component at `src/components/tags.js`
182+
183+
```jsx
184+
import React from 'react';
185+
import Link from 'gatsby-link';
186+
187+
export default function Tags({ list = [] }) {
188+
return (
189+
<ul className="tag-list">
190+
{list.map(tag =>
191+
<li key={tag}>
192+
<Link to={`/tags/${tag}`}>
193+
{tag}
194+
</Link>
195+
</li>
196+
)}
197+
</ul>
198+
);
199+
}
200+
```
201+
202+
We can now utilize this new component on the blog home page by passing in our tags
203+
204+
```jsx
205+
const IndexPage = ({ data }) => (
206+
<div>
207+
<h1>My Travel Blog</h1>
208+
{data.allMarkdownRemark.edges.map(({ node }) =>
209+
<div key={node.id}>
210+
<h3>
211+
{node.frontmatter.title}
212+
</h3>
213+
<Tags list={node.frontmatter.tags || []} />
214+
</div>
215+
)}
216+
</div>
217+
)
218+
```

0 commit comments

Comments
 (0)