要为每个类别使用单独的帖子模板,您需要在函数中添加以下函数。php:
/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . \'/single\');
/**
* Filter the single_template with our custom function
*/
add_filter(\'single_template\', \'my_single_template\');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
if(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\'))
return SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\';
elseif(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\'))
return SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\';
endforeach;
这段代码将告诉脚本查找模板,例如single cat uncategorized。php或single-cat-1。php位于/单个文件夹中。您必须确保将文件保存在需要在主题目录中创建的单个文件夹中。
https://codex.wordpress.org/Post_Type_Templates