Last active
September 6, 2016 07:35
-
-
Save ShinichiNishikawa/25ff4e795f3d00ed5b94 to your computer and use it in GitHub Desktop.
Load template file in plugin folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class nskw_template_loader { | |
public function __construct() { | |
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) ); | |
} | |
public static function template_loader( $template ) { | |
// テンプレートファイルの場所 | |
$template_dir = plugin_dir_path( __DIR__ ) . 'templates/'; | |
if ( is_search() && 'estate' == $_GET['s'] ) { | |
// 探すべきファイル名 | |
$file_name = 'search-estate.php'; | |
} elseif ( is_singular( 'estate' ) ) { | |
$file_name = 'single-estate.php'; | |
} | |
if ( isset( $file_name ) ) { | |
// テーマ(子 → 親)のファイルを先に探す | |
$theme_file = locate_template( $file_name ); | |
} | |
if ( isset( $theme_file ) && $theme_file ) { | |
$template = $theme_file; | |
} elseif ( isset( $file_name ) && $file_name ) { | |
$template = $template_dir . $file_name; | |
} | |
return $template; | |
} | |
} | |
new nskw_template_loader(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment