繁体   English   中英

使用自定义模板的Wordpress用于子类别和其中的帖子

[英]Wordpress using custom template for sub-sub-category and posts within

有一个大的通用类别Catalog$category->cat_ID = 2 )。 它包含一些子类别,例如Series1Series2等。 每个子类别都包含一些子类别,例如Type1Type2等。每个子类别都包含一些产品(type 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');

但我不知道如何根据自己的需要对其进行自定义。

UPD。 我已经写了下面的代码

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(); if(0!= $ this_category-> parent){//来自404.php的代码}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM