For styles wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
然后在希望加载CSS的任何位置使用:wp_enqueue_style('namespace');
.
脚本与上面相同,但是加载jQuery的更快方法是只使用init中加载的enqueue来加载您想要加载的页面:wp_enqueue_script('jquery');
Unless of course you want to use the google repository for jquery.
You can also conditionally load the jquery library that your script is dependent on:
wp_enqueue_script('namespaceformyscript', 'http://locationofscript.com/myscript.js', array('jquery'));
Update Sept. 2017
I wrote this answer a while ago. I should clarify that the best place to enqueue your scripts and styles is within the wp_enqueue_scripts
hook. So for example:
add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');
function callback_for_setting_up_scripts() {
wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
wp_enqueue_style( 'namespace' );
wp_enqueue_script( 'namespaceformyscript', 'http://locationofscript.com/myscript.js', array( 'jquery' ) );
}
The wp_enqueue_scripts
action will set things up for the "frontend". You can use the admin_enqueue_scripts
action for the backend (anywhere within wp-admin) and the login_enqueue_scripts
action for the login page.