2012-05-25 73 views
0

我想創建一個插件,當我創建一個子類別時,該插件將動態創建一個類別模板名稱的類別模板。動態創建Wordpress類別模板

就像我有一個類別的新聞。爲此,我有一個模板「category-news.php」。現在我在名爲'最新消息'的新聞下創建一個新的子類別。爲此,我想在我的主題文件夾中創建一個實用名爲'category-latest-news.php'的文件,並將category-news.php模板代碼放入其中。

+0

這個插件的目的是所有的子類別都應該遵循父類別的模板。我在找什麼,我發現這篇文章http://stackoverflow.com/questions/3114934/make-wordpress-subcategories-use-category-template。 –

回答

0

您可以使用php的fopen和fwrite創建一個文件,然後以編程方式將模板名稱插入到文件的頂部。

爲了得到當前頁面的父ID:

$parents = get_post_ancestors($post->ID); 
$parentsid = $parents[count($parents)-1]; 

然後你可以使用代碼Make Wordpress subcategories use Category Template

function myTemplateSelect() { 
    if (is_category() && !is_feed()) { 
     if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) { 
      load_template(TEMPLATEPATH . '/category-projects.php'); 
      exit; 
     } 
    } 
} 

add_action('template_redirect', 'myTemplateSelect'); 
0

使用模板過濾器

add_filter('template_include', 'template_include', 10); 

和更改模板as

function template_include($template) 
{ 

    if(your condition here){ 
     $template = get_template_directory().'/your-template.php'; 
    } 

    return $template; 
}