2013-01-17 36 views
2

是否可以根據您所在的類別顯示不同的主題?根據類別使用不同的WordPress主題

例如:

blog.example.com/category/foo -> style1. 
blog.example.com/category/bar -> style2. 

谷歌搜索帶來了this page,他建議在single.php中這樣做:

$post = $wp_query->post; 
if (in_category(5)) { 
    include(TEMPLATEPATH . '/style1.php'); 
} else { 
    include(TEMPLATEPATH . '/style2.php'); 
} 

這是要走的路?

回答

2

您不應該爲此使用include。 Wordpress只有在這些情況下才有令人驚喜的功能get_template_part。它有一個內置的回退機制,並與你的主題很好地集成:

$categories = get_the_category(); 

get_template_part('for-category', $categories[0]->slug); 

然後,只是名稱與類別蛞蝓文件:

blog.example.com/category/foo - >對於類-foo.php
blog.example.com/category/bar - >換品類bar.php

請記住,也有一個默認的for-category.php文件,這樣就可以回退到該如果你沒有習慣文件爲當前類別。

+0

謝謝約瑟夫,我非常感謝你的幫助! – dallen

+0

@dallen - 當然。請注意,您鏈接到的文章早在2007年就有了,在Wordpress引入'get_template_part'函數之前。雖然'get_template_part'並不是全新的,我鏈接的文章在印刷機上很熱(今天早上)。絕對值得一讀。 –