Is it possible to have different layouts, theme or links depending on the category the user has clicked?
This question comes from Milo Riano. Milo wants to be able to have what I call a “flexi-site”. A Flexi-site is a site that is flexible in terms of display options or layouts. This could really be an extension of the question about multiple blogs as well.
Template Files
The key here is in template file hierarchy. WordPress looks in the theme directory to find the following files, and in this order:
- category-X.php
- category.php
- archive.php
- index.php
So if you have a blog that you assign a category of “Linkblog” to, and you want the linkblog to be displayed in a different format that the rest of the blog, you could simply find the ID number of the category (We’ll call it 23 for the sake of argument), and you could create a special template file with the unique layout you wish to use, and call the file category-23.php. Now everytime the category page is loaded, the template file category-23.php will be used.
Custom CSS
You can take this approach a step farther through customized CSS stylesheets. As most templates use the inbuilt function <php get_header() ?> to summon the use of the common header.php file from the theme, it is also possible to create custom headers and include those directly.
However, the better solution to this, providing you don’t have a vast number of categories that you want to display different CSS files for, is to simply use the WordPress template tag is_category() to figure out whether to display an alternate stylesheet.
<php
if( is_category( 23 ) )
{
?>
<link rel="stylesheet" href="http://example.com/wp-content/theme/cat23.css" type="text/css" media="screen" />
<php
}
else
{
?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<php
}
?>
You can get real creative with this instead choosing to use is_category() to determine if extra CSS can be added after the main CSS thus overriding styles. There are quite a few options and you are limited only by your creativity.
If you have questions for this ongoing WordPress FAQ series, shoot me an email and let me know. By doing so, you’ll get a free link to your site.
Table of contents for WordPress FAQ
- WordPress FAQ: How Do I combine Blogs?
- WordPress FAQ: What’s up with the Amazon Plugin with WP 2.1.x?
- WordPress FAQ: How Do I Use Category Themes?
- WordPress FAQ: Where did my Preview Link Go?
- WordPress FAQ: How Do I Use Child Pages More Effectively?
- WordPress FAQ: How Do I Fix the Blogroll Category Issue in WordPress 2.1
- WordPress FAQ: How do I Move my blog to a new host?
- WordPress FAQ: User Roles Confusion
- WordPress FAQ: What is the best way to upgrade a WordPress 1.5 blog to WordPress 2.1?
- WordPress FAQ: Democracy Poll Feature
- WordPress FAQ: Benefits of Tagging
- WordPress FAQ: What’s the Best Way to Backup my Blog?
- WordPress FAQ: How Do I Integrate WordPress Into a Non-Blog Site?
- WordPress FAQ: Troubleshooting a WordPress Install


