根据您的问题,我猜您希望在不更改模板(或其名称)的情况下分配模板。有一个过滤器single_template
. 它用于拦截WP的标准系统,即为帖子找到模板。您可以将其包含在functions.php
像这样(未经测试):
function wpse254523_single_template ($single_template) {
global $post;
$cat = get_the_category($post->ID);
if ($cat == \'cat1\' || $cat == \'cat2\')
$single_template = dirname( __FILE__ ) . \'/single1.php\';
elseif ($cat == \'cat3\')
$single_template = dirname( __FILE__ ) . \'/single2.php\';
return $single_template;
}
add_filter( \'single_template\', \'wpse254523_single_template\' );