2014-10-27 97 views
2

我将bootstrap主题转换为wordpress。现在,我在加载引导脚本和样式时遇到了问题。 这是我的functions.php如何在functions.php(wordpress)中加载引导脚本和样式?

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_enqueue_script('samplejs', get_template_directory_uri() . '/js/jquery.min.js', array('')); 
      wp_enqueue_script('samplejs', get_template_directory_uri() . '/js/clean-blog.min.js', array('')); 
     // For either a plugin or a theme, you can then enqueue the script: 
     wp_enqueue_script('custom-script'); 
    } 

add_action('wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery'); 

代码,我加入这在我的头

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

其他问题: 是什么wp_register和wp_enqueue之间的区别? 请帮帮我。 Im初学者使用bootstrap。

回答

5

所有你需要做的就是排队他们。另外,我建议摆脱你的jQuery导入(第二个wp_enqueue)。 WordPress默认包含jQuery,并且您已经将它包含在第一个脚本中(因为您已将其列为依赖项)。下面是一个例子,但这排入jQuery的两倍(本地jQuery和引导jQuery的):

function wpbootstrap_scripts_with_jquery() 
{ 
    // Register the script like this for a theme: 
    wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/bootstrap.js', array('jquery')); 
    wp_enqueue_script('bootstrap-jquery', get_stylesheet_directory_uri() . '/js/jquery.min.js'); 
    wp_enqueue_script('blog-scripts', get_stylesheet_directory_uri() . '/js/clean-blog.min.js'); 
} 

add_action('wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery'); 

你需要注册脚本前排队-ING他们来说,是如果其中一个脚本的唯一原因是一种依赖性。从文档:

wp_register_script()注册在WordPress脚本文件稍后将使用该wp_enqueue_script()函数,它安全地 处理脚本依赖链接 到页面。

已使用wp_register_script预注册脚本()做 并不需要使用手动wp_enqueue_script(),如果他们被列为正在排队的另一个脚本的依赖 排队。 WordPress 将在包含 排队脚本之前自动包含注册脚本,该脚本将注册脚本的句柄列为 依赖项。

+0

非常感谢你@rnevius :)怎么样的风格? – akselrows 2014-10-27 09:02:58

+0

我用更多的解释编辑了我的答案。对于enqueueing样式,可以使用'wp_enqueue_style'在相同的函数中以相同的方式包含它们。 – rnevius 2014-10-27 09:04:04

+0

嗯。我尝试了代码,但效果仍然不起作用 – akselrows 2014-10-27 09:06:30

2

您使用“wp_enqueue_style”和“wp_enqueue_script”。

例如:

function reg_scripts() { 
    wp_enqueue_style('bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css'); 
    wp_enqueue_style('bootstrapthemestyle', get_template_directory_uri() . '/css/bootstrap-theme.min.css'); 
    wp_enqueue_script('bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true); 
} 
add_action('wp_enqueue_scripts', 'reg_scripts'); 
+0

谢谢@elnaz :) actulally它的工作现在,但由于jQuery冲突,联系表单7不工作。请帮我解决这个问题。 – akselrows 2014-10-27 09:26:48

相关问题