值仅显示来自echo

时间:2014-04-03 13:50:50

标签: php wordpress

这可能是一个非常简单的问题,如果是的话我很抱歉。但是我一直在寻找并且看不到解决方案。

我使用wordpress作为CMS,并根据变量将Google字体排入队列

if (!function_exists('opd_load_google_style'))  {
    /* Add Google Fonts */
    global $opd_albaband;
    $google_font = $opd_albaband['typography_h1']['font-family'];

    function opd_load_google_style() {
        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
    add_action('wp_enqueue_scripts', 'opd_load_google_style');
}

然而,这会产生未定义的变量$google_font。我可以使用$google_font显示echo $google_font,但这不适用于wp_register_style ...

我错过了什么吗?对不起它的简单。

2 个答案:

答案 0 :(得分:0)

由于opd_load_google_style功能无法访问$google_font,您只需稍微改变一下即可。

if (!function_exists('opd_load_google_style'))  {
    function opd_load_google_style() {
        /* Add Google Fonts */
        global $opd_albaband;

        $google_font = $opd_albaband['typography_h1']['font-family'];
        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
}

答案 1 :(得分:0)

您的$google_font变量超出了范围,因此您无法在opd_load_google_style函数中使用它。相反,您的代码应该如下所示

if (!function_exists('opd_load_google_style'))  {

    function opd_load_google_style() {

        /* Add Google Fonts */
        global $opd_albaband;
        $google_font = $opd_albaband['typography_h1']['font-family'];

        if (!is_admin()) {
            wp_register_style('googleFont','http://fonts.googleapis.com/css?family='.$google_font.' 400,700');
            wp_enqueue_style('ggl', get_stylesheet_uri(), array('googleFont') );
        }
    }
    add_action('wp_enqueue_scripts', 'opd_load_google_style');
}

或者您可以通过

$google_font变量传递给您的函数
function opd_load_google_style($google_font) {
    //Inside your function you now have access to $google_font
}
相关问题