Now that you have individual pages for every tag, let’s make links to them.
Get ready to…
- Add a new page using the
- Display a list of all your unique tags, linking to each tag page
- Update your site with navigation links to this new Tags page
Use the Section titled Use the /folder/index.astro routing pattern
/folder/index.astro routing pattern
To add a Tag Index page to your website, you could create a new file at
But, since you already have the directory
/tags/, you can take advantage of another routing pattern in Astro, and keep all your files related to tags together.
Try it yourself - Make a Tag Index pageSection titled Try it yourself - Make a Tag Index page
Create a new file
index.astroin the directory
localhost:3000/tagsand verify that your site now contains a page at this URL. It will be empty, but it will exist.
Create a minimal page at
src/pages/tags/index.astrothat uses your layout. You have done this before!
Expand to see the steps
Create a new page component in
Show the filename
Import and use your
Show the code
Define a page title, and pass it to your layout as a component attribute.
Show the code
Check your browser preview again and you should have a formatted page, ready to add content to!
Create an array of tagsSection titled Create an array of tags
You have previously displayed items in a list from an array using
map(). What would it look like to define an array of all your tags, then display them in a list on this page?
See the code
You could do this, but then you would need to come back to this file and update your array every time you use a new tag in a future blog post.
Fortunately, you already know a way to grab the data from all your Markdown files in one line of code, then return a list of all your tags.
src/pages/tags/index.astro, add the line of code to the frontmatter script that will give your page access to the data from every
.mdblog post file.
See the code
src/pages/tags/[tag].astroto return a list of unique tags.
Create your list of tagsSection titled Create your list of tags
Instead of creating items in an unordered list this time, create one
<p> for each item, inside a
<div>. The pattern should look familiar!
Add the following code to your component template:
In your browser preview, verify that you can see your tags listed.
To make each tag link to its own page, add the following
<a>link to each tag name:
Add styles to your tag listSection titled Add styles to your tag list
Add the following CSS classes to style both your
<p>that will be generated. Note: Astro uses HTML syntax for adding class names!
Define these new CSS classes by adding the following
<style>tag to this page:
Check your browser preview at
localhost:3000/tagsto verify that you have some new styles and that each of the tags on the page has a working link to its own individual tag page.
Code Check-InSection titled Code Check-In
Here is what your new page should look like:
Add this page to your navigationSection titled Add this page to your navigation
Right now, you can navigate to
localhost:3000/tags and see this page. From this page, you can click on links to your individual tag pages.
But, you still need to make these pages discoverable from other pages on your website.
Navigation.astrocomponent, include a link to this new tag index page.
Show me the code
Challenge: Include tags in your blog post layoutSection titled Challenge: Include tags in your blog post layout
You have now written all the code you need to also display a list of tags on each blog post, and link them to their tag pages. You have existing work that you can reuse!
Follow the steps below, then check your work by comparing it to the final code sample.
- Copy the
src/pages/tags/index.astroand reuse it inside
Before this code will work, you need to make one small edit to the code you pasted into
MarkdownPostLayout.astro. Can you figure out what it is?
Give me a hint
How are the other props (e.g. title, author, etc.) written in your layout template? How does your layout receive props from an individual blog post?
Give me another hint!
In order to use props (values passed) from a
.md blog post in your layout, like tags, you need to prefix the value with a certain word.
Show me the code!
Code Check-in: MarkdownPostLayoutSection titled Code Check-in: MarkdownPostLayout
To check your work, or if you just want complete, correct code to copy into
MarkdownPostLayout.astro, here is what your Astro component should look like:
Test your knowledgeSection titled Test your knowledge
Match each file path with a second file path that will create a page at the same route.