您可以使用此代码,它首先在主题文件夹中查找,如果找不到,则在插件文件夹中查找。
Sample code:
add_action( \'template_redirect\', \'weinvent_template_redirect\' );
/*Template fallback*/
function weinvent_template_redirect() {
global $wp, $wp_query;
$plugindir = dirname( __FILE__ );
if( is_attachment() ) {
$templatefilename = \'image.php\';
if( file_exists( TEMPLATEPATH . \'/\' . $templatefilename ) ) {
$return_template = TEMPLATEPATH . \'/\' . $templatefilename;
} else {
$return_template = $plugindir . \'/templates/\' . $templatefilename;
}
do_theme_redirect( $return_template );
}
}
function do_theme_redirect( $url ) {
global $post, $wp_query;
if( have_posts() ) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}