下面是我要做的,来整合head。js公司:
我将此代码放在模板中functions.php
文件
define(\'THEME\', get_bloginfo(\'template_url\'), true);
define(\'THEME_JS\', THEME . \'/js/\', true);
add_action(\'wp_print_scripts\',\'head_js_files\');
function head_js_files()
{
if (is_admin()) return; //to preserve the admin
global $wp_scripts;
$in_queue = $wp_scripts->queue;
if(!empty($in_queue))
{
$scripts = array();
foreach($in_queue as $script)
{
$src = $wp_scripts->registered[$script]->src;
$src = ( (preg_match(\'/^(http|https)\\:\\/\\//\', $src)) ? \'\' : get_bloginfo(\'url\') ) . $src;
$scripts[] = \'{\' . $script . \':"\' . $src . \'"}\';
}
echo "<script type=\\"text/javascript\\" src=\\"".THEME_JS."head.js\\"></script>\\n";
echo "<script type=\\"text/javascript\\">head.js(\\n". implode(",\\n", $scripts). "\\n);</script>\\n";
}
$wp_scripts->queue = array();
}
它输出如下内容:
<script type="text/javascript">head.js(
{jquery:"/wp-includes/js/jquery/jquery.js"},
{jquery_lastfm:"http://localhost/lucianomammino/wp-content/plugins/lastfm-recent-tracks-widget/js/jquery.lastfm.js"},
{nav:"http://localhost/lucianomammino/wp-content/themes/lmtheme/js/jquery.dropdown.js"}
);</script>
请注意,它还使用了脚本标签,有时这对于确定加载了什么脚本(以及何时加载)非常有用。