dsfsdf7852 2015-09-04 11:41 采纳率: 100%
浏览 39

Wordpress使用自定义模板为子子类和内部的帖子

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;
  • 写回答

1条回答 默认 最新

  • duandui5648 2015-09-04 11:54
    关注

    I would recommend against using a 404, as that implies something different, and specific; 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. Something like this:

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

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP