更新时间: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.
wp_enqueue_script()
-注意WP自动加载的脚本列表,其中包括jQuery get_current_screen()