使用版本变量将样式入队

时间:2018-07-13 作者:Carl Johnston

我的版本变量似乎没有在我的排队函数中应用(下面的代码片段)。

//version number
$version = \'1.0.0\';

function frontend_styles($version) {
    wp_enqueue_style(
        \'frontend_styles\', //reference
        get_stylesheet_directory_uri() . \'/assets/css/app.min.css\', //source
        array(), //dependenices
        $version, //version number
        \'all\' //media type (\'all\', \'screen\', \'handheld\', \'print\')
    );
}
是否有方法设置可应用于所有样式的全局版本号?

编辑:下面是实际的源代码。

//version number
$version = \'1.0.0\';

//remove jquery scripts
function remove_jquery() {
    wp_deregister_script(\'jquery\');
    wp_deregister_script(\'jquery-core\');
    wp_deregister_script(\'jquery-migrate\');
}

//remove embed scripts
function remove_embed() {
    wp_deregister_script(\'wp-embed\');
}

//site styles
function frontend_styles() {
    global $version;
    wp_enqueue_style(
        \'frontend_styles\', //reference
        get_stylesheet_directory_uri() . \'/assets/css/app.min.css\', //source
        array(), //dependenices
        $version, //version number
        \'all\' //media type (\'all\', \'screen\', \'handheld\', \'print\')
    );
}

//admin_styles
function backend_styles() {
    global $version;
    wp_enqueue_style(
        \'backend_styles\', //reference
        get_stylesheet_directory_uri() . \'/assets/css/admin.min.css\', //source
        array(), //dependenices
        $version, //version number
        \'all\' //media type (\'all\', \'screen\', \'handheld\', \'print\')
    );
}

//site scripts
function frontend_scripts() {
    global $version;
    wp_enqueue_script(
        \'frontend_scripts\', //reference
        get_stylesheet_directory_uri() . \'/assets/js/app.min.js\', //source
        array(), //dependencies
        $version, //version number
        true //load in footer
    );

    //pass sylsheet uri to javascript variable in app.js
    $translation_array = array(
        \'get_stylesheet_directory_uri\'  => get_stylesheet_directory_uri() . \'/\',
        \'home_url\'                      => home_url(\'/\')
    );

    wp_localize_script(\'frontend_scripts\', \'php\', $translation_array);
}

//admin scripts
function backend_scripts() {
    global $version;
    wp_enqueue_script(
        \'backend_scripts\', //reference
        get_stylesheet_directory_uri() . \'/assets/js/admin.min.js\', //source
        array(), //dependencies
        $version, //version number
        true //load in footer
    );
}

2 个回复
SO网友:Florian

Try this

// global theme version
$version = \'1.0.0\';

function frontend_styles() {

   global $version;
    wp_enqueue_style(
        \'frontend_styles\', //reference
        get_stylesheet_directory_uri() . \'/assets/css/app.min.css\', //source
        array(), //dependenices
        $version, //version number
        \'all\' //media type (\'all\', \'screen\', \'handheld\', \'print\')
    );
}
SO网友:Jacob Peattie

您的变量未在中定义scopefrontend_styles() 作用

如果您定义$version 在函数外部,在全局范围内,您需要使用global 要访问它的函数内的关键字:

//version number
$version = \'1.0.0\';

function frontend_styles() {
    global $version;

    wp_enqueue_style(
        \'frontend_styles\', //reference
        get_stylesheet_directory_uri() . \'/assets/css/app.min.css\', //source
        array(), //dependenices
        $version, //version number
        \'all\' //media type (\'all\', \'screen\', \'handheld\', \'print\')
    );
}

结束

相关推荐