You may have certain categories in your WordPress blog that you want to appear differently than other categories. For example, what if you have a category called “Tomatoes” and a category called “Cucumbers,” and you want the titles of every post in the Tomatoes category to be red, and the titles in the Cucumbers category to be green?
There are a few plugins that can help you with this, but they didn’t work so smoothly. The best solution I have found is one provided by Lorelle on her Taking Your Camera on the Road blog. In her post titled “Different Category – Different Look: Creating Multiple Single Posts Looks for Different Categories,” she gives the following directions:
We began by making two back up copies of the single.php page called single1.php and single.2.php.
Inside of the original single.php, delete everything and replace it with this:
<?php
$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>In the most simple terms, the PHP code issues a query that says “Check the post. If the post is in category ID number 9, display single2.php. If not in category ID number 9, display single1.php.”
In the in_category(), we set the category ID number to 9, the one that holds all of my web page design articles and experiments.
So basically, your main single.php file becomes a conditional file that tells WordPress which template file to use in certain categories. Lorelle also gives the following code you can put in the single.php file that will allow you to create multiple styles for multiple categories:
<?php
$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single9.php');
elseif ( in_category('12') ) {
include(TEMPLATEPATH . '/single12.php');
elseif ( in_category('42') ) {
include(TEMPLATEPATH . '/single42.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
Now you have to get the styles you want to apply to the correct category. Do the following for every category you want to have a certain style for:
- Create a new style sheet and call it style-9.css (9 is the category ID, but your category ID will likely differ. You can still call it style-9.css, but this is confusing!) If the changes you are making are minor, like changing certain colors or fonts, you should probably just copy style.css and rename it to style-9.css. In that style sheet, create the styles that you want, so for example if you want all h2 items to be red, make those changes in your new style sheet.
- Style sheets are called in your header.php file. Which means that we need to create a header file that will call your new style sheet. So copy your header.php and rename the copy header-9.php (or whatever you want).
- In your new header-9.php file, find the lines that look something like this:
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css" type="text/css" media="screen" />
and change where it saysstyle.css
tostyle-9.css
. - Then, in your single2.php file, replace the following line (which is probably the first line):
<?php get_header(); ?>
with this
<?php include ('header-hebrew.php'); ?>
Do this for every category. If you have a lot of categories you want to appear with different styles, there’s probably some conditional PHP that you can use. Being of little PHP knowledge, I don’t know what this code would be, but the above works quite nicely for people like me with little-to-no knowledge of PHP and only one or two categories that need modifying.
Pingback: ??????? 16.07.07 : ????? ?? ??"? ???()
Pingback: Giving different styles to different WordPress categories » WordPressGarage.com()
Pingback: Deluxe : The creative practice of Sean Gallagher()