2013-03-14 12 views
1

我在WordPress的發現,使用相同的類別模板孩子和大孩子類別的最佳方式是就在這裏計算器: https://stackoverflow.com/a/3117202/391929WordPress模板所有子類而不是父

但如何使JUST子類使用該模板而不是基礎父類別?

我試過編輯只是如果這樣一行,從這樣的:

if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) 

這樣:

if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) 

但它不工作,爲什麼?

回答

2

我設法在一個程序的方式來解決這個問題:

1主題功能添加了這個片斷:

add_action('template_redirect', 'load_category_tree_template'); 
function load_category_tree_template() { 
     if (is_category() && !is_feed()) { 
      // replace 'your-base-category-slug' with the slug of root category 
      $base_tree_cat_id = get_cat_id('your-base-category-slug'); 
       // get current category id 
      $catid = get_query_var('cat'); 

      if (is_category($base_tree_cat_id) || cat_is_ancestor_of($base_tree_cat_id, $catid)) { 
       load_template(STYLESHEETPATH . '/category-your-base-category-slug.php'); 
       exit; 
      } 
     } 
    } 

然後在品類的根類,slug.php文件I包括根據目前的類別不同鼻涕蟲其他文件:

<?php get_header(); ?> 

<?php 
if (is_category()) { 
    $cat = get_query_var('cat'); 
    $currentcat = get_category ($cat); 
    $currentslug = $currentcat->slug; 
} 
// if we are in base category we know it by the slug 
if ($currentslug == 'your-base-category-slug'){ 
     // if we are load a file with the loop for the root category 
    require_once('post-loop-caregory-tree-root.php'); 
}else{ 
     // otherwise we are not in the root, so show the loop for all other category in the tree 
    require_once('post-loop-category-tree.php'); 
} 
?> 

<?php get_footer(); ?> 

我知道這是可以做的更好,但它最終很簡單的解決方案。

+0

由於沒有其他的建議,我保持我的答案是好的,但我確信這可以用更好的方式完成...... – bluantinoo 2013-03-22 16:45:21

相關問題