我有一个插件,其中包含一个PHP模板single_template
和archive_template
对于多个帖子类型。我正在这个模板中加载客户端应用程序,这就是为什么我只需要为所有四个变体使用一个模板的原因。
我的代码是:
add_filter( \'single_template\', \'react_template\' );
add_filter( \'archive_template\', \'react_template\' );
function react_template( $template ) {
global $post;
if ( $post->post_type == \'tutorial\' || $post->post_type == \'dashboard\' ) {
if ( file_exists( PLUGIN_DIR . \'templates/learning.php\' ) ) {
return PLUGIN_DIR . \'templates/learning.php\';
}
}
return $template;
}
这完全符合我的需要。但是,如果tutorial
或dashboard
类型我收到错误,因为$post
为空。这没什么大不了的,因为在生产中,系统中总会有很多帖子,但这让我很烦。我不想为每种类型都指定一个命名模板。在没有帖子的情况下,有没有办法让我的归档模板工作?