K2 Templating: using a different category_item.php

 In this tutorial we go over using the category_item.php files in different scenarios.

Since K2 templates are often more or less static once setup, it isn’t the end of the world to have certain elements set within the template files.  Let’s say that you want to have a different template for items that are being pulled from a subcategory “web” in your category listing.  You can via ID or via alias define which subcategory items are rendered using different category_item.php (or in this case category_web.php)

 For this tutorial, we’re assuming this category structure:

  • – Blog
  • – – Seattle
  • – – Web
  • – – Design

Below is the code stucture that we’re aiming for:

 <loop leading items>
<if item category alias == 'web'>
<display category_web.php>
<display category_item.php>
<end if>
<end loop>

Below is an actual example from what I did on ryokokawa.com

I used the system to display 2 different types of posts in addition to the normal “category_item.php” post types (snapshot and picture-book).  Below code is what is being used within the “category.php” file to switch the item listing template.

<?php if(isset($this->leading) && count($this->leading)): ?>
<div id="item-list" class="span12">
<div class="row">
<?php foreach($this->leading as $key=>$item): ?>
// Load category_item.php by default
if($item->categoryalias == 'snapshot') {
echo $this->loadTemplate('snapshot');
} elseif($item->categoryalias == 'picture-book') {
echo $this->loadTemplate('book');
} else {
echo $this->loadTemplate('item');
<?php endforeach; ?>
<div class="clearfix"></div>
<?php endif; ?>

Hope this was helpful!