Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
The Category Template2:04 with Zac Gordon
Having the ability to click on a blog post category and see other posts with the same category helps the user experience as well as search engine optimization. Adding a categories archive template will allow us to do this. It will function the same way as the main blog landing page, but filter results by the selected category.
For project files please refer to the first video: Coding the Header and Footer Templates
In the last video, we learned how to echo out the categories for a blog post.
Now we're going to look at how to create a category.php page
and customize what a blog category listing page looks like.
In the last video, we finished up the blog section of our site
for the listing and individual blog post pages.
We also added the ability to output the categories for each blog post.
However, when we click on a category,
it shows the correct blog post, but we lose the formatting we want.
The reason for this is that because WordPress can't find a category.php page
or an archive.php page, it's using the index.php page as its backup.
We can see that by adding in "Index" to the top and seeing that show up on our page.
Because we want a more customized category page,
we're going to open up our home.php page and save it as category.php.
Now when we view our page, we should see that it appears in the proper formatting.
One thing that would be nice, though, is if at the top,
we could say that you're currently viewing category design.
To accomplish this, we use a new tag called single_cat_title.
We can add this to the top of our page inside of an h1 tag
and say Category single_cat_title.
WordPress is smart enough to know that we're currently on the Design section,
and it's only showing design posts.
When we click on a post, it'll bring us into the proper area
where we can read the full post and leave comments if we like.
You need to sign up for Treehouse in order to download course files.Sign up