我有一个简单的问题,我希望有人可以解释一下。我有一个带有自定义页面模板的子主题,我只是想检查天气与否模板正在使用中。在正常情况下,我会使用is_page_template
函数,但它似乎不适用于子主题。我试过以下
if(is_page_template('template-custom-fullwidth.php')){
//do something..
}
以及
if(is_page_template(dirname(get_bloginfo('stylesheet_url')).'/template-custom-fullwidth.php'){
//do something..
}
Neiter工作,我希望有一个比使用$_SERVER
检查URL更优雅的解决方案。我无法想象没有这种看法的功能,因为这似乎是一项常见的任务。我相信问题是模板和样式表目录之间的区别。是否可以使用Wordpress检查位于子主题中的页面模板?
提前致谢!
答案 0 :(得分:2)
我遇到了类似的问题。一些试验和错误注意到,如果条件保留在函数内部,它可以工作。但如果留在外面则没有。
function team_page_enqueue_style() {
if ( is_page_template('page-team.php') ) {
wp_enqueue_style( 'easy-responsive-tabs-css', get_stylesheet_directory_uri() . '/css/easy-responsive-tabs.css', array(), NULL);
}
}
add_action( 'wp_enqueue_scripts', 'team_page_enqueue_style' );
答案 1 :(得分:1)
如果在循环内使用is_page_template,请在调用is_page_template之前调用wp_reset_query()
。
答案 2 :(得分:0)
尝试is_singular();它适用于我的案例,因为我的模板是一个帖子页模板。
要使用它,您需要指定不带单词single-
和.php
的模板名称。例如,如果模板文件是single-forest_of_trees.php
,那么这应该是代码:
if (is_singular( 'forest_of_trees' )) {
// do something
}
它还允许多种值的优雅方式。
答案 3 :(得分:0)
有同样的问题,并解决了这个问题:
[[[[verify(mockManager)
withMatcher:anything() forArgument:0]
withMatcher:anything() forArgument:1]
withMatcher:anything() forArgument:2]
updateFeaturesButtons:0 category:0 parentId:0 success:(id)captor failure:anything()];
无效,因为它会显示网址并且您需要服务器的完整路径,请使用get_stylesheet_directory_uri()
如果get_stylesheet_directory()
不起作用,您可以使用is_page_template()
并比较
get_page_template()
答案 4 :(得分:0)
对我有用的是(在functions.php中):
if (get_page_template_slug() == 'template-custom-fullwidth.php'){
// anything
}
答案 5 :(得分:0)
在我的子主题中,唯一对我有用的方法是访问全局$ template变量并与之进行比较:
global $template;
if (basename($template) === 'template-custom-fullwidth.php') {
// do something
}
答案 6 :(得分:0)
使用主题文件夹中的相对路径:
if(is_page_template('page-templates/template-custom-fullwidth.php')){
//do something..
}
答案 7 :(得分:-1)
参考此WP Codex页面:http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
请改用get_stylesheet_directory_uri
。这应该有效:
is_page_template(get_stylesheet_directory_uri() . '/template-custom-fullwidth.php')
{
//do something..
}