我在his blog 它允许您基于类别(slug和id)、作者或标记创建单个帖子模板。
我修改的代码如下:
/**
* 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 custom taxonomy \'event_types\'
* Change \'event_types\' to whatever your custom taxonomy is called
* Check by category slug and ID
*/
//Only change \'event\' post type
if( \'event\' != get_post_type($post) )
return $single;
//Where your single fiels are located inside your theme
$single_template_path = get_stylesheet_directory().\'/single/\';
//Get the taxonomy terms
$cats = get_the_terms($post->ID,\'event_types\');
if ( !$cats )
return $single;
foreach( $cats as $cat ) :
if(file_exists($single_template_path . \'/single-cat-\' . $cat->slug . \'.php\'))
return $single_template_path . \'/single-cat-\' . $cat->slug . \'.php\';
elseif(file_exists($single_template_path . \'/single-cat-\' . $cat->term_id . \'.php\'))
return $single_template_path . \'/single-cat-\' . $cat->term_id . \'.php\';
endforeach;
return $single;
}
然后将新的自定义模板文件添加到
/wp-content/themes/your-template-name/single, 相应地命名它们。
使用此函数,可以为名为featured 在自定义分类中event_types 通过创建名为single-cat-featured.php