WP_ADD_INLINE_STYLE()中的未定义变量
我将网站更新为php8,并在最后一行中使用WP_ADD_INLINE_STYLE('theme-style',$ customVariables)在最后一行中获取警告“ Undefinited $ 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 );
}
I updated the website to PHP8 and getting the Warning "Undefined variable $customVariables" on the last line with wp_add_inline_style( 'theme-style', $customVariables );
The code is below:
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 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您在 block中仅定义
$ customvariables
。在情况下,$ headings_font或$ body_font
评估false
变量将为undefined
。您可以更新此信息:
或者您可以将
WP_ADD_INLINE_STYLE
移动到If Block:this is because you only define
$customVariables
inside yourif
block. In the case$headings_font or $body_font
evaluates tofalse
the variable will beUndefined
.You can update to this:
or you can move
wp_add_inline_style
to inside the if block: