問題描述
我使用 Chip Bennett 的 well-written 釀酒學主題作為我自己的兒童主題的父母。
在我的開發過程中,我發現有人在編寫 「兒童」 主題時遇到了一些挑戰。
我剛剛發現我的主要 style.css 檔案在< head> 之前的每個其他樣式錶連結或語句之前被載入,這就解釋了為什麼我無法覆蓋一些父樣式。
進一步研究問題表明,各種父樣式表和樣式可以在< head> 在三個地方 add_action('wp_print_styles',,add_action('wp_enqueue_scripts',,然後 add_action('wp_head', 。
為了保持簡單,我打算建立兩個樣式表。第一個主要的’style.css’ 表將只包含載入 Oenology 主要樣式表所需的 @import url()命令。
第二個樣式表將包含我的子規則。要確保它在所有其他規則之後載入,我將使用 add_action( 'wp_head', 排隊。
這聽起來很合理嗎?還是有更好的 (更正確的) 方法呢?
btw,有誰知道 「/parent-theme/style.css?MRPreviewRefresh = 723」 是什麼意思?
Update
wp_enqueue_style() 在 wp_head() 中似乎沒有工作。
歡呼,格雷戈裡
最佳解決方案
只是在這個問題上,這個問題可能是邊界過於本地化的,因為它是專門針對釀酒學主題的。
也就是說,這裡是我認為你有問題的地方:釀酒學排列了兩種樣式表:
-
style.css,直接在檔案頭 (因此在wp_head()被燒) -
{varietal}.css,wp_enqueue_scripts,優先11,functions/dynamic-css.php:/** * Enqueue Varietal Stylesheet */ function oenology_enqueue_varietal_style() { // define varietal stylesheet global $oenology_options; $oenology_options = oenology_get_options(); $varietal_handle = 'oenology_' . $oenology_options['varietal'] . '_stylesheet'; $varietal_stylesheet = get_template_directory_uri() . '/varietals/' . $oenology_options['varietal'] . '.css'; wp_enqueue_style( $varietal_handle, $varietal_stylesheet ); } // Enqueue Varietal Stylesheet at wp_print_styles add_action('wp_enqueue_scripts', 'oenology_enqueue_varietal_style', 11 );
應用”skin” 的品種樣式表以優先順序 11 進行排隊,以確保首先載入基本樣式表 style.css,然後載入多樣式樣式表,以便導致正確的級聯。
所以,如果你需要覆蓋多樣的樣式表,只需要在多樣化樣式表之後排列你的第二個樣式表; 即具有至少 12 或更高的優先權。
Edit
要提供一個 more-general 答案,more-general 問題:
為了覆蓋父主題樣式表入庫,您需要知道兩件事情:
-
樣式表排入佇列的動作鉤子
-
將回撥新增到掛鉤的優先順序
可以在 init 鉤子和 wp_print_scripts /wp_print_styles 鉤子之間的任何位置正確執行排隊功能 (wp_enqueue_script() /wp_enqueue_style()) 。 (執行 wp_enqueue_*()功能的語義正確的鉤子目前是 wp_enqueue_scripts) 。 This list includes the following actions(其中,這些只是通常的嫌疑人):
-
init -
wp_head -
wp_enqueue_scripts -
wp_print_scripts/wp_print_styles
(請注意,wp_enqueue_scripts,wp_print_styles 和 wp_print_scripts 都在 wp_head 內部全部起火,具體優先。
因此,為了覆蓋 Parent-Theme 樣式表,您需要執行以下操作之一:
-
De-enqueue Parent-Theme 樣式表,透過
wp_dequeue_style( $handle ) -
刪除透過
remove_action( $hook, $callback )排列樣式的父主題回撥 -
使用 CSS 級聯來覆蓋 Parent-Theme 樣式表,透過將您的 Child-Theme 樣式表
wp_enqueue_style()呼叫掛鉤到具有較低優先順序的相同鉤子或稍後的鉤子中。對於最後一個選項,如果父主題使用:
`add_action( 'wp_enqueue_scripts', 'parent_theme_enqueue_style', $priority )`… 然後兒童主題將使用:
`add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_style', {$priority + 1} )`
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。