如何将外部JavaScript文件链接到WordPress主题?
问题描述:
我无法通过插件将外部JavaScript文件链接到我的WordPress主题。所有我发现是这样附加的JavaScript文件:如何将外部JavaScript文件链接到WordPress主题?
function theme_name_scripts() {
wp_register_script('script-name-faizan-test', 'http://vjs.zencdn.net/4.12/video.js');
wp_enqueue_script('script-name-faizan-test');
}
add_action('wp_enqueue_scripts', 'theme_name_scripts');
我在做什么错?
答
According to the WP Codex,示例给出了额外的参数:
<?php wp_register_script($handle, $src, $deps, $ver, $in_footer); ?>
所以:
function child_add_scripts() {
wp_register_script(
'google-analytics',
'http://google.com/analytics/script.js',
false,
'1.0',
true
);
wp_enqueue_script('google-analytics');
答
入住这link出来:
请注意,您的相对路径可能会有所不同。
function wptuts_scripts_basic()
{
// Register the script like this for a plugin:
wp_register_script('custom-script', plugins_url('http://vjs.zencdn.net/4.12/video.js', __FILE__));
// or
// Register the script like this for a theme:
wp_register_script('custom-script', get_template_directory_uri() . 'http://vjs.zencdn.net/4.12/video.js');
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script('custom-script');
}
add_action('wp_enqueue_scripts', 'wptuts_scripts_basic');
你把它放在哪里?你怎么知道它不工作? – atmd