且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery冲突!在WordPress的POST EDIT页面上显示白屏

更新时间:2023-12-01 22:50:28

如何添加脚本WordPress方式:

How to add scripts The WordPress Way:

add_action( 'wp_enqueue_scripts', 'pjso_my_scripts' );
function pjso_my_scripts() {
    $handle = 'script_name';
    $src = '/path/to/script.js';
    $deps = array( 
        'jquery',
    );
    // add any other dependencies to the array, using their $handle
    $ver = 'x.x';  // if you leave this NULL, 
                   // then it'll use WordPress's version number
    $in_footer = true; // if you want it to load in the page footer
    wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
}

要仅在后端的编辑帖子"屏幕上执行此操作,可以将admin_enqueue_scripts挂钩与get_current_screen()结合使用:

To do it only on the Edit Post screen in the backend, you can use the admin_enqueue_scripts hook combined with get_current_screen():

add_action( 'admin_enqueue_scripts', 'pjso_my_scripts' );
function pjso_my_scripts() {
    $screen = get_current_screen();
    if( 'post' != $screen->base || 'edit' != $screen->parent_base ) {
        return; // bail out if we're not on an Edit Post screen
    }
    $handle = 'script_name';
    $src = '/path/to/script.js';
    $deps = array( 
        'jquery',
    );
    // add any other dependencies to the array, using their $handle
    $ver = 'x.x';  // if you leave this NULL, 
                   // then it'll use WordPress's version number
    $in_footer = true; // if you want it to load in the page footer
    wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
}

应该有效,尽管我尚未对其进行测试.

That should work, though I haven't tested it.

此代码可以放入主题的functions.php文件中,也可以编写自定义插件,它将在以后的任何主题更改中保持不变.

This code can go in your theme's functions.php file, or you could write a custom plugin so that it will persist across any future theme changes.