【问题标题】:Broken functions.php; how to Move scripts From bootstrap into WordPress?损坏的functions.php;如何将脚本从引导程序移动到 WordPress?
【发布时间】:2013-11-15 18:12:13
【问题描述】:

我是 bootstrap 和 WordPress 的新手,并试图组合一个简单的页面。因此,从页眉和页脚添加和注册脚本时遇到依赖问题并不让我感到惊讶。

这里有什么明显的我遗漏的吗?

<?php 

function wpbootstrap_scripts_with_jquery()
{

// register the script like this for a theme:
    wp_register_script( 'custom-script', get_template_directory_uri() . 'js/bootstrap.js', array( 'jquery' ) );     
    wp_register_script( 'jquery-tweet', get_template_directory_uri() . 'js/jquery.tweet.js', array( 'jquery' ) );
    wp_register_script( 'clock', get_template_directory_uri() . 'js/clock.js', array( 'jquery' ) ); 
    wp_register_script( 'soon', get_template_directory_uri() . 'js/soon.js', array( 'jquery' ) );

//footer scripts     
    wp_register_script( 'dat-gui-min-js', get_template_directory_uri() . 'js/dat.gui.min.js' );
    wp_register_script( 'fss', get_template_directory_uri() . 'js/fss.js');
    wp_register_script( 'bgCustom', get_template_directory_uri() . 'js/bgCustom.js');


// For either a plugin or a theme, you can then enqueue the script:
    wp_enqueue_script( 'custom-script','jquery-tweet','clock','soon','dat-gui-min-js','fss','bgCustom');
}

add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );

?>

在 header.php 中

<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>

【问题讨论】:

  • 提醒一下.. 你不需要 在标题中了。 WP 自动加载最新的 jQuery。

标签: javascript wordpress twitter-bootstrap


【解决方案1】:

我相信您在wp_register_script 注册的每个脚本都需要一个唯一的名称。例如,您将它们全部命名为“custom_script”。我会将每个文件命名为与其 JavaScript 文件相同的名称(只是为了方便)。比如……

wp_register_script( 'jquery-tweet', get_template_directory_uri() . 'js/jquery.tweet.js', array( 'jquery' ) );
wp_register_script( 'clock', get_template_directory_uri() . 'js/clock.js', array( 'jquery' ) );

...等等。然后您将像这样将每个人排入队列...

wp_enqueue_script( 'jquery-tweet' );
wp_enqueue_script( 'clock' );

...等等

希望有帮助,玩得开心!

【讨论】:

  • 不确定它是否重要,但可能会将它们放在 add_action 函数中,这样它们就可以挂在正确的位置,而不仅仅是 functions.php 加载。
  • 是的,完全正确。 (我的 sn-ps 不能按原样工作......它们只是 OP 代码上下文中的 sn-ps,所以仍然需要 add_action 调用。感谢您的澄清。
  • 谢谢,我会继续努力的。对于 add_action... 这与
  • @MackieeE np,我提交了对代码块的格式更改,所以它可能更具可读性。 :)
  • @Charles 尝试将所有wp_enqueue_script 函数调用放入 wpbootstrap_scripts_with_jquery() 函数(而不是放在header.php 文件中)。我通常把我的放在最后,这样就很容易找到它们,就在那些 wp_register_script 函数调用之后。
猜你喜欢
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2016-05-27
相关资源
最近更新 更多