的兩大部件得到一個腳本加載:
<?php wp_register_script('script-handle', 'script-location'); ?>
和
<?php wp_enqueue_script('script-handle'); ?>
wp_enqueue_script不會加載腳本,除非它已先行註冊(注意,WP附帶了許多預註冊腳本,如jQuery的,所以在這種情況下,所有你需要做的是排隊劇本。但是,對於未綁定在WP中的自定義腳本,您需要先註冊它)。
因此,基於這個問題,假設你已經將腳本放在你的THEME目錄下的文件夾「includes」中(例如wp-content/themes/my-theme/includes/links.js ),那麼這就是你會在你的主題的functions.php文件做什麼:
<?php
function my_theme_register_scripts() {
wp_register_script('my-script-handle', get_bloginfo("template_url") . '/includes/links.js');
wp_enqueue_script('my-script-handle');
}
// If you want to use the script on the FRONT end of the site, then....
add_action('init', 'my_theme_register_scripts');
// If you want to use the script on the BACK end of the site....
add_action('admin_init', 'my_theme_register_scripts');
?>
而且大衛 - 是正確的 - 你需要確保你的主題既有wp_head()和wp_footer()調用在其中,否則的東西不會按預期的那樣工作...
給我們看一些代碼... – 2012-02-02 16:51:53