2015-09-04 75 views
1

有一個很大的共同類別Catalog$category->cat_ID = 2)。它包含幾個子類別,如Series1,Series2等。每個子類別包含一些子子類別,如Type1,Type2等。每個子子類別包含一些產品(類型post)。
我不需要顯示常見類別Catalog及其子類別,但我需要在各個頁面上顯示其子產品的子類別。
如何設置404.php模板類別Catalog及其子類別和每個子子類別(產品列表)和每個產品的自定義模板?WordPress的使用自定義模板的子分類和帖子

我發現下面的代碼(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_templatesub-categories了。如何爲普通類別Catalog及其子類別設置404.php

UPD2我發現在我的代碼錯誤,它需要

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

回答

0

我會建議不要使用404,因爲這意味着不同的東西,和具體的;該內容不存在或不可用,而不是該內容是該分類的孩子。

但是,如果你想......只需從你的404.php中複製代碼並將它嵌套在查詢類別+ 1級別的條件語句中。事情是這樣的:

如果(is_category( '目錄')){$ = this_category get_queried_object(); 如果(0!= $ this_category->父){// 從404.php 代碼}}

+0

我更新了我的問題,你可以檢查一下,好嗎? – hronikata

相關問題