1

我正在研究一个遗留的 wp 主题,并且有一些我想应用到设置模板的可爱干净的新 CSS 和 js。

我已经设法使用下面的脚本从应用到我的新模板中删除了旧的 CSS 和 js。

但是,如何在删除旧的同时向这些函数添加新的脚本和样式?

例如,删除所有旧的 CSS 和 js 现在添加这些 js 和 CSS

    /**
 * Remove all java scripts.
 */
function se_remove_all_scripts() {
    global $wp_scripts;
    if ( is_page_template( 'new-template/page-main.php' ) ) {
        $wp_scripts->queue = array();
    }
}

/**
 * Remove all style sheets.
 */
function se_remove_all_styles() {
    global $wp_styles;
    if ( is_page_template( 'new-template/page-main.php' ) ) {
        $wp_styles->queue = array();
    }
}

add_action( 'wp_print_styles', 'se_remove_all_styles', 99 );

add_action( 'wp_print_scripts', 'se_remove_all_scripts', 99 );
4

1 回答 1

0

functions.php您可以从 enqueue 函数中省略旧样式和脚本,并在文件中为条件队列创建一个新样式和脚本。看到这个:

function my_conditional_enqueue() {
    if ( is_page_template( 'new-template/page-main.php' ) {
        /** Call your new style and script files */
    } else {
        /** Call legacy files */
    }
}
add_action( 'wp_enqueue_scripts', 'my_conditional_enqueue' );
于 2019-07-18T14:50:30.760 回答