Custom Category Templates on a Archive or Index page.

Posted on Wednesday the 23rd of January, 2008 at 5:00 pm in Dev, WordPress

On my home page and in my archives, I use a custom category template to display asides and news articles. This is very easy to do and it only takes a couple seconds of work to create custom category templates in any WordPress theme.

The first step is to add the following to your current theme’s index.php loop after the line that looks like <?php while (have_posts()) : the_post(); ?>, but before any other code.

  1. <?php $cat_temp = cat_loop();?>
  2. <?php if($cat_temp && is_numeric($cat_temp)){?>
  3. <?php include('loops/cat_'.$cat_temp.'.php');?>
  4. <?php }else{ ?>

Then add } just before the line endwhile.

The next step is to add the following to your theme’s functions.php file (you may have to create a file with the same name):

  1. function cat_loop(){
  2. global $blog_id,$post, $wp_version;
  3. if($wp_version >= 2.3){
  4. global $object_term_cache;
  5. $array = $object_term_cache[$blog_id][$post->ID]['category'];
  6. }else{
  7. global $category_cache;
  8. $array = $category_cache[$blog_id][$post->ID];
  9. }
  10. while (list($cat) = each($array)) {
  11. if(file_exists(dirname(__FILE__).'/loops/cat_'.$cat.'.php')){
  12. return $cat;
  13. }
  14. }
  15. }
  16. Use the following to copy and paste the code.

This can be modified to look at author’s also. Now the only thing you have to do is to create a folder named “loops” in your theme’s folder, and then create a new file with a new “loop” — excluding the while and endwhile parts — and name it cat_xxx.php where xxx is the id of the category the loops is for.

This can be repeated for single.php, archive.php, or anywhere else a custom loop is useful. You can even use something similar for individual posts or pages.

A post may be in multiple categories, but it will only use the first custom template it finds, so make sure the post is in only one specially styled category at a time.

Related posts

  1. ttn mobile posted the following on February 6, 2009 at 9:11 am.

    Great Article. Thanks. I agree

    Reply to ttn mobile
  2. stumbler posted the following on April 2, 2009 at 12:33 am.

    The conditional ‘if’ tests the WP version, specifically 2.3. I have 2.6, however, and want to stay with that, at least until I have nailed down all else that I want on my site.

    My main interest (and block) is that I need a category-specific header (it only displays large-font text and a navbar listing all five categories, with “home” replacing the category that is being viewed at the time.)

    Content for each category is linear, much like a textbook is linear content flow, and all category content follows a chapter-logic which are sub and sub-sub etc. categories.

    Any suggestion on how to do this in general, or how your code should be modified to accomplish this?

    Really like your site, the design as well as your interdisciplinary content.

    Reply to stumbler

Leave a reply

:) :D :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: ;) :!: :?: :idea: :arrow: :| :mrgreen: