我將網站更新為 PHP8,并在最后一行使用 wp_add_inline_style('theme-style', $customVariables); 收到警告“未定義變數 $customVariables”;代碼如下:
function spectra_custom_styles($custom) {
//Fonts
$headings_font = esc_html(get_theme_mod('spectra_headings_fonts'));
$body_font = esc_html(get_theme_mod('spectra_body_fonts'));
if ( $headings_font or $body_font) {
$customVariables = ":root{"."\n";
if ( $headings_font ) {
$font_pieces = explode(":", $headings_font);
$customVariables .= "--c7-heading-font-family: {$font_pieces[0]};"."\n";
}
if ( $body_font ) {
$font_pieces = explode(":", $body_font);
$customVariables .= "--c7-font-family: {$font_pieces[0]};"."\n";
}
$customVariables .= "}";
}
//Output all the styles
wp_add_inline_style( 'theme-style', $customVariables );
}
uj5u.com熱心網友回復:
這是因為您只在塊$customVariables內定義。if在這種情況下,對變數的$headings_font or $body_font評估false將是Undefined.
你可以更新到這個:
function spectra_custom_styles($custom) {
//Fonts
$headings_font = esc_html(get_theme_mod('spectra_headings_fonts'));
$body_font = esc_html(get_theme_mod('spectra_body_fonts'));
$customVariables = "";
if ( $headings_font or $body_font) {
$customVariables .= ":root{"."\n";
if ( $headings_font ) {
$font_pieces = explode(":", $headings_font);
$customVariables .= "--c7-heading-font-family: {$font_pieces[0]};"."\n";
}
if ( $body_font ) {
$font_pieces = explode(":", $body_font);
$customVariables .= "--c7-font-family: {$font_pieces[0]};"."\n";
}
$customVariables .= "}";
}
//Output all the styles
wp_add_inline_style( 'theme-style', $customVariables );
}
或者您可以移動wp_add_inline_style到 if 塊內部:
function spectra_custom_styles($custom) {
//Fonts
$headings_font = esc_html(get_theme_mod('spectra_headings_fonts'));
$body_font = esc_html(get_theme_mod('spectra_body_fonts'));
if ( $headings_font or $body_font) {
$customVariables = ":root{"."\n";
if ( $headings_font ) {
$font_pieces = explode(":", $headings_font);
$customVariables .= "--c7-heading-font-family: {$font_pieces[0]};"."\n";
}
if ( $body_font ) {
$font_pieces = explode(":", $body_font);
$customVariables .= "--c7-font-family: {$font_pieces[0]};"."\n";
}
$customVariables .= "}";
//Output all the styles
wp_add_inline_style( 'theme-style', $customVariables );
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466201.html
標籤:WordPress 定制 php-8 内联样式 未定义的变量
下一篇:Powershell:通過Register-ObjectEvent注冊的事件處理程式不會立即觸發-僅在我的對話框關閉后
