Today I learned about category templates in WordPress. There is a simple, yet rarely used feature, that allows you to display content based on a specific category. For example, if you have 10 categories, with ID’s 1-10, and you want to show your visitors something different for each of these 10 categories, you can create files with this naming convention:
category-1.php, category-2.php, and so on.
When the URL http://mydomain.com/category/coolcat is entered into the browsers address bar, and WordPress sees a category request made, it looks for a category-x.php file in the main template folder. If one exists, that will be used over the default template file. If a template file isn’t found for the requested category, the next file that WordPress looks for is category.php. This is another template default file that can be applied to all categories that don’t have their own template. If a category.php isn’t found, then the default template file is used.
One thing I discovered while trying to get category templates to work, I was entering http://mydomain.com/coolcat and expecting the category template to display. But as you may know, WordPress was trying to find a Page named “coolcat”. I struggled with this for a week, and after ripping out the small hair follicles on my head, it dawned on me I was entering the wrong URL. Remember, /category/ must precede the category name, or it just doesn’t work.
Here is a link to the WordPress Codex explaining category template.
Jeez, that makes that big if/then thing we made a few weeks ago look a lot simpler...
ReplyDeleteYeah, it's a feature I over looked. But now I know. Will this make that feature for your site work better?
ReplyDeleteAs I was thinking further, you could just add the different sidebar index, like sidebar 1 and 2 for cat 1, sidebar 3 and 4 for cat 2, and so on.
Yeah, i know Wp is amazing, and alwaya offering new features for us. Like me, i never stopped learning something new about WP.
ReplyDeleteYeah, i know Wp is amazing, and always offering new features for us. Like me, i never stopped learning something new about WP.
ReplyDelete