简体   繁体   English

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

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

There is a big common category Catalog ( $category->cat_ID = 2 ). 有一个大的通用类别Catalog$category->cat_ID = 2 )。 It contains a few sub-categories like Series1 , Series2 and so on. 它包含一些子类别,例如Series1Series2等。 Every sub-category contains a few sub-sub-categories like Type1 , Type2 , etc. And every sub-sub-category contains some products (type post ). 每个子类别都包含一些子类别,例如Type1Type2等。每个子类别都包含一些产品(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. 我不需要显示公共类别Catalog及其子类别,但是我需要在各个页面上显示其产品的子类别。
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? 如何设置类别Catalog及其子类别的404.php模板以及每个子类别(带有产品列表)和每个产品的自定义模板?

I have found code below ( functions.php file) 我在下面找到了代码( 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');

but I don't know how to customize it for my needs. 但我不知道如何根据自己的需要对其进行自定义。

UPD. 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. 但它也将catalog_template用于sub-categories How to set 404.php for common category Catalog and for its sub-categories? 如何为通用类别Catalog及其子类别设置404.php

UPD2 I've found a mistake in my code, it needs to be UPD2我在代码中发现了一个错误,它必须是

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

I would recommend against using a 404, as that implies something different, and specific; 我建议不要使用404,因为这意味着有所不同和具体。 the content is not there or is unavailable, not the content is a child of this taxonomy. 内容不存在或不可用,不是内容是此分类法的子级。

BUT, if you wanted to..... Just copy the code from your 404.php and nest it in a conditional statement that queries the category + 1 level down. 但是,如果您想.....只需从404.php复制代码并将其嵌套在查询类别+ 1级以下的条件语句中即可。 Something like this: 像这样:

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

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

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