There is a big common category Catalog
($category->cat_ID = 2
). It contains a few sub-categories like Series1
, Series2
and so on. Every sub-category contains a few sub-sub-categories like Type1
, Type2
, etc. And every sub-sub-category contains some products (type post
).
I don't need to show common category Catalog
and its sub-categories, but I need to show sub-sub-category with its products on individual pages.
How to set 404.php
template for category Catalog
and its sub-categories and custom templates for every sub-sub-categories (with list of products) and for every product?
I have found code below (functions.php
file)
function catalog_template() {
// Get the category id from global query variables
$cat = get_query_var('cat');
if(!empty($cat)) {
// Get the detailed category object
$category = get_category($cat);
// Check if it is sub-category and having a parent, also check if the template file exists
if( ($category->parent != '0') && ($category->parent == '2') && (file_exists(TEMPLATEPATH . '/catalog.php')) ) {
// Include the template for sub-catgeory
include(TEMPLATEPATH . '/catalog.php');
exit;
}
return;
}
return;
}
add_action('template_redirect', 'catalog_template');
but I don't know how to customize it for my needs.
UPD. I've written the code below
function catalog_template() {
// Get the category id from global query variables
$cat = get_query_var('cat');
if(!empty($cat)) {
$catalog_id = get_cat_ID('Catalog');
$catalog_child_cats = array();
foreach(get_all_category_ids() as $child_cat)
{
if(get_category($child_cat)->parent==$catalog_id)
{
$catalog_child_cats[]=$child_cat;
}
}
// Get the detailed category object
$category = get_category($cat);
$cat_parent_id = $category->cat_ID;
// Check if it is sub-category and having a parent, also check if the template file exists
if( (in_array($cat_parent_id, $catalog_child_cats)) && (file_exists(TEMPLATEPATH . '/catalog.php')) ) {
// Include the template for sub-sub-catgeory
include(TEMPLATEPATH . '/catalog.php');
exit;
}
return;
}
return;
}
add_action('template_redirect', 'catalog_template');
but it uses catalog_template
for sub-categories
too. How to set 404.php
for common category Catalog
and for its sub-categories?
UPD2 I've found a mistake in my code, it needs to be
// Get the detailed category object
$category = get_category($cat);
$cat_parent_id = $category->category_parent;