WP_ADD_INLINE_STYLE()中的未定义变量

发布于 2025-01-23 21:58:09 字数 931 浏览 4 评论 0原文

我将网站更新为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-01-30 21:58:09

这是因为您在 block中仅定义$ customvariables。在情况下,$ headings_font或$ 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 Block:

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 );
        
    }

}

this is because you only define $customVariables inside your if block. In the case $headings_font or $body_font evaluates to false the variable will be Undefined.

You can update to this:

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 );
}

or you can move wp_add_inline_style to inside the if block:

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 );
        
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文