我正在尝試處理我的儿童主题,但我無法删除"飞天飞車" 警告,该警告在我啟用child主题後立即出現。 阅讀其他問题後,我了解到filemtime警告通常是由於URL和路徑使用不正確引起的.但是,我认為我正確提供了URL和路徑,如果是這樣,那麼我還缺少其他內容。
此功能来自父主题:
filemtime(): stat failed
這是从我的child主题出發的:
function mtt_styles()
{
wp_enqueue_style(
'mtt-custom-style',
get_stylesheet_directory_uri() . '/dist/css/custom-style.css',
false,
filemtime(get_stylesheet_directory() . '/dist/css/custom-style.css'),
'all'
);
wp_enqueue_style('mtt-main-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'mtt_styles');
当我进入頁面源時,我看到排队工作了.所有檔案都在那裏,並且父主题中的所有樣式都可以在子級中很好地工作,但是警告仍然存在。
有人可以帮助我在這裏發現我的缺失或做錯什麼吗?
function child_styles()
{
$theme = wp_get_theme();
wp_enqueue_style(
'mtt-custom-style',
get_template_directory_uri() . '/dist/css/custom-style.css',
array(),
filemtime(get_template_directory() . '/dist/css/custom-style.css'),
'all'
);
wp_enqueue_style(
'mtt-main-style',
get_template_directory_uri() . '/style.css',
array(),
$theme->parent()->get('Version'),
'all'
);
wp_enqueue_style(
'child-main-style',
get_stylesheet_uri(),
array('mtt-custom-style', 'mtt-main-style'));
}
add_action('wp_enqueue_scripts', 'child_styles');
- 6月前1 #
- 6月前2 #
這裏有两个主要問题:
問题1:get_template__....
和get_stylehsheet_...
不一樣filemtime(get_template_directory() . '/dist/css/custom-style.css'),
表示获取父主题dist/css/custom-style.css
的檔案修改時間 檔案。我怀疑,父主题中没有這樣的檔案,因為它仅存在於子主题中,因此PHP会發出警告,要求您為不存在的檔案修改檔案時間。
get_template_directory
和get_stylesheet_directory
不要做同一件事。get_template_directory
是父主题(如果不使用子主题,則為当前主题get_stylesheet_directory
是当前主题,又稱為子主题以
問题2:對get_template_...
開頭的所有其他功能也是如此 和get_stylesheet_...
functions.php
的誤解 子主题和父主题以及子主题的工作原理。如果使用子主题,則父主题為
functions.php
仍然執行.子主题不允许您通過在子主题中放置一个具有相同名稱的新檔案来覆盖任何檔案.仅適用於通過get_template_part
載入的模板A PHP file in a child theme, or a JS/CSS file in a child theme does not override the same file in the parent theme.
因此,如果您將CSS檔案放入父主题中,請複製
functions.php
更改為子主题並對其进行更改,它没有覆盖父主题.相反:both
functions.php
檔案執行所有重複的代碼現在執行两次
原始JS以及您尝試用其覆盖的新JS檔案都已入队
相似問題
- wordpress:获取父主题版本wordpresschildthemewordpresswpenqueuestylewordpressparentthemewordpresswpgettheme2020-12-20 05:58
- wordpress:使父Fontawesome出佇列出双父樣式wordpresscsswordpresschildthemewordpresswpenqueuestyle2020-11-28 15:24
- wordpress:如何在子主题中為require_once指定路徑?wordpressphpwordpressthemedevelopmentwordpresschildtheme2020-07-19 15:25
- wordpress:警告:printf():helpersphp檔案中的引數太少wordpressphpwordpressthemedevelopmentwordpressthemeswordpresschildtheme2020-06-28 11:56
- wordpress:如何在wordpress(Oceanwp)中將多个樣式表放入我的子主题中wordpresschildthemewordpresswpenqueuestyle2020-05-29 19:22
問题出在您的父主题中.父主题正在使用
get_stylesheet_directory()
在這裏:当父主题處於活動狀態時,這很好,因為
get_stylesheet_directory()
会將该檔案指向父主题.問题是,当您啟用子主题時,它試圖获取filemtime()
'/dist/css/custom-style.css'
的 在您的子主题中,我猜该檔案在那裏不存在.因此失败了。問题在於,因為
filemtime()
立即執行,重新定義指令碼的URL或將其出队無關紧要,因為它已经尝試過並且無法檢查時間,並丟擲錯誤。如果您是父主题的作者,那麼解決問题就很簡單,只需替換
get_stylesheet_directory()
与get_template_directory_uri()
(或者更好,get_parent_theme_file_path()
).這樣,在載入缺少该檔案的樣式表時就不会發生该錯誤,並且您無需从该子主题重新為其添加元素。如果您不是原始作者,那麼正確的解決方案是仅解開
mtt_styles()
来自wp_enqueue_scripts
完全.然後从子主题開始,使用正確的路徑重新加入佇列.您已经在做後者了,所以您只需要做摘钩的部分.诀窍在於您需要从after_setup_theme
內部將其解钩 钩子,因為否則將不会再添加原始钩子,因為先載入了子主题: