flaga Treść tego wpisu jest dostępna również w języku polskim

Most WorPress users still use categories to assign particular post to pre-defined subject. Sometimes we would like to emphase this using different color for post title or background. In other cases we want to skip the date of publication or remove comments template from posts belonging to particular category.
It’s much easier to achieved then you may think although it requires some work [nothing is easy ;)].
I will explain you how to do that in index.php file.
That’s the_loop from default Kubrick theme:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author() ?></small>
<div class="entry"> <?php the_content('Read the rest of this entry &raquo;'); ?> </div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
</div>
<?php endwhile; ?>

First we need to introduce variables depending on categories. We use function in_category(‘ID’) where ID need to be replaced with number of category we want to apply this variable to. Then in style.css file we need to add necessary stylization like different font or background color.

Example #1
In cats category (ID=2) posts won’t have author’s name published and instead of full content only excerpt will show up on main page. Our Loop should look like that (in bold are pieces of code that have changed):

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( in_category('2') ) { ?>
<div class="cats" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry"> <?php the_excerpt(); ?> </div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
<?php }
else { ?>

<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry"> <?php the_content('Read the rest of this entry &raquo;'); ?> </div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
<?php } ?>
</div>
<?php endwhile; ?>

Example #2
In Pratchett category (ID=3) post title should be green (#006633) and comments template should have different text:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( in_category('3') ) { ?>
<div class="terry" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry"> <?php the_excerpt(); ?> </div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No reaction &#187;', '1 reaction &#187;', '% actions ;) &#187;'); ?></p>
<?php }
else { ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry"> <?php the_content('Read the rest of this entry &raquo;'); ?> </div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
<?php } ?>
</div>
<?php endwhile; ?>

In style.css we add:
.terry h2 {
color: #006633;
}

If we want to stylize more categories the_loop should look like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( in_category('2') ) { ?>
<div class="cats" id="post-<?php the_ID(); ?>">
...ble-ble-ble...
<?php }
elseif (in_category('3')) { ?>
<div class="terry" id="post-<?php the_ID(); ?>">
...ble-ble-ble...
<?php }
elseif (in_category('4')) { ?>
<div class="books" id="post-<?php the_ID(); ?>">
...ble-ble-ble...
<?php }
else { ?>
<div class="post" id="post-<?php the_ID(); ?>">
..ble-ble-ble...
<?php } ?>
</div>
<?php endwhile; ?>

And that’s all. Simple and efficient although you need to spend some time on it ;).