【发布时间】:2016-09-12 17:33:40
【问题描述】:
我想使用样式表检查特定主题是否是子主题。
例如:
$childBoolean = functionHere( 'twentyfifteen' );
我需要某种函数来检查特定主题是否是孩子。它应该返回一个布尔值。
那么,有没有一个函数可以检查它?有什么想法吗?
谢谢
【问题讨论】:
标签: wordpress themes parent-child stylesheet
我想使用样式表检查特定主题是否是子主题。
例如:
$childBoolean = functionHere( 'twentyfifteen' );
我需要某种函数来检查特定主题是否是孩子。它应该返回一个布尔值。
那么,有没有一个函数可以检查它?有什么想法吗?
谢谢
【问题讨论】:
标签: wordpress themes parent-child stylesheet
如果您知道如何使用此代码,它可能会很有用:
1486 // Look in a parent theme first, that way child theme CSS overrides.
1487 if ( is_child_theme() ) {
1488 $template_uri = get_template_directory_uri();
1489 **$template_dir = get_template_directory();**
1490
1491 foreach ( $editor_styles as $key => $file ) {
1492 if ( $file && file_exists( "$template_dir/$file" ) ) {
1493 $stylesheets[] = "$template_uri/$file";
1494 }
1495 }
1496 }
(来自:wp-includes/theme.php,通过 AllysonSouza 评论中的链接。 我想您应该能够使用其中一些 php 命令或提取变量来获取模板目录名称,这几乎总是主题名称。
$template_dir 或 get_template_directory();
此外,可能会使用以下内容:
128 /**
129 * Whether a child theme is in use.
130 *
131 * @since 3.0.0
132 *
133 * @return bool true if a child theme is in use, false otherwise.
134 **/
135 function is_child_theme() {
136 return ( **TEMPLATEPATH** !== STYLESHEETPATH );
137 }
我想象的相关变量(?)是 TEMPLATEPATH。
【讨论】:
您可以使用is_child_theme() 来检查当前主题是否是另一个主题的子主题。函数文档在这里:https://codex.wordpress.org/Function_Reference/is_child_theme
【讨论】: