是否有本机函数来检查主题是否是模板文件。例如,如果主题未使用“主页”。php文件,然后执行一些代码。。。
如何检查WordPress模板文件是否存在?
2 个回复
最合适的回答,由SO网友:Webord 整理而成
因此,我想补充以下内容:
function foo_function() {
$located = locate_template( \'home.php\' );
if ( !empty( $located ) ) {
// \'home.php\' found in Theme, do something
}
}
add_action(\'init\', \'foo_function\');
// remember to change both of the parameters above, first one for where you want the
// action to happen and the second one the name of the function declared
正如“Chip Bennett”所说,它将检查两个TEMPLATEPATH
和STYLESHEETPATH
, 但我会将代码附加到挂钩上,而不是将其放在functions.php
文件但这取决于你。
SO网友:Chip Bennett
任何原因locate_template()
(Codex ref) 不起作用?
if ( \'\' != locate_template( \'home.php\' ) ) {
// \'home.php\' found in Theme, do something
}
请注意locate_template()
将同时检查TEMPLATEPATH和STYLESHEETPATH,因此它适用于独立主题和子主题。结束