Create WordPress custom template based on category slug

To create a custom template in WordPress for specific categories based on the slug is not a built-in function.  Not sure why since it makes sense to build this capability without creating a new filter.  This could be plug-in, but it’s so simple, it’s one of those things you wonder why it’s taking so long to be included in the core capabilities.  Who wants to install all those single-purpose plug-ins, anyway?

So if you have a category slug called “news” and you want all your news posts to use this new template, just add this filter to your child theme’s footer.php file.  In this example, you would need to create a template file called “single-news.php” and WordPress will automatically use this to override the theme’s template for that category.

NOTE: If you’re not using a child theme, read this first, then if you’re still not convinced, use:   TEMPLATEPATH  instead of STYLESHEETPATH for above.  But don’t ever do it that way, every time WordPress gets updated you’ll end up having to make these changes, unless you just like letting code get broken because you’re too insecure of a web developer to want to create new solutions so you’d prefer to just keep fixing things you could prevent from breaking…

Aaron Belchamber has written 243 articles

Leave a Reply

Your email address will not be published. Required fields are marked *