Wordpress使用自定义模板进行子类和帖子的子类分类


Wordpress using custom template for sub-sub-category and posts within

有一个大的共同类别Catalog ($category->cat_ID = 2)。它包含Series1Series2等几个子类别。每个子类别包含几个子类别,如Type1, Type2等。每个子类都包含一些产品(post类型)。
我不需要显示常见类别Catalog及其子类别,但我需要在各个页面上显示其子类别及其产品。
如何为Catalog类别及其子类别设置404.php模板,并为每个子类别(含产品列表)和每个产品设置自定义模板?

我在下面找到了代码(functions.php文件)

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');

但是我不知道如何根据我的需要定制它。

乌利希期刊指南。我写了下面的代码

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');

,但它使用catalog_template sub-categories也。如何为常见类别Catalog及其子类别设置404.php ?

UPD2我在我的代码中发现了一个错误,它应该是

// Get the detailed category object
    $category = get_category($cat);
    $cat_parent_id = $category->category_parent;

我不建议使用404,因为它暗示了一些不同的,特定的东西;内容不存在或不可用,而不是内容是此分类法的子分类法。

但是,如果你想.....只需从404.php中复制代码,并将其嵌套在查询类别+ 1级别的条件语句中。像这样:

if(is_category('Catalog')){$this_category = get_queried_object();如果(0 != $this_category->parent){//代码来自404.php}}