Using in_category To Show Different Template For Different Category In WordPress
When developing sites under wordpress, we, sometime came across condition where we need to show different templates for a particular category/categories. For example, I have a client that need a particular template for his website that is based on wordpress. The site is about restaurant that he own. It has 3 type of posts, regular post for news & events, store items, and menu. For regular post, well, there should be no problem, but for menu, there are several custom fields in actions and the same for store items.
I can go using several if..else or switch(). But, that would produce a bulky single.php which, I try to avoid. There goes, until I found out the simple solution on codex, that is using in_category(). The page also teach me how to implement it smartly. The solution is rather simple. All you have to do is, create several php files which contain codes and add this code inside your single.php:
<?php get_header();
$recipeArrays = array(6,7,8,9,10);
$cateringArrays = array(12,13,14,15,16,17);
$menuArrays = array(18,19,20,21,22,23);
$storeArrays = array(25,26,27,28,29,30);
if(in_category($recipeArrays))
{
include('single_inner_recipe.php');
}
else if(in_category($cateringArrays))
{
include('single_inner_catering.php');
}
else if(in_category($menuArrays))
{
include('single_inner_menu.php');
}
else if(in_category($storeArrays))
{
include('single_inner_store.php');
}
get_footer(); ?>
That's it, now you have a more manageable codes.
10 Most Popular Search Terms
-
Matt








