如何在functions.php(wordpress)中加载bootstrap脚本和样式?

我将bootstrap主题转换为wordpress。 现在,我在加载引导脚本和样式时遇到问题。 这是我在functions.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_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' ); 

我在我的标题中添加这个

  

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

您需要做的就是将它们排队。 另外,我建议摆脱你的jquery导入(第二个wp_enqueue)。 WordPress默认包含jQuery,你已经将它包含在第一个脚本中(因为你已将它列为依赖项)。 这是一个例子,但是这会将jquery排队两次(本机jquery和bootstrap 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' ); 

在排队之前需要注册脚本的唯一原因是,其中一个脚本是依赖项。 来自文档:

wp_register_script()在WordPress中注册一个脚本文件,以便稍后使用wp_enqueue_script()函数链接到页面,该函数可安全地处理脚本依赖项。

使用wp_register_script()预先注册的脚本不需要使用wp_enqueue_script()手动排队,如果它们被列为排队的另一个脚本的依赖项。 WordPress将自动包含已注册的脚本,然后它包含排队的脚本,该脚本将注册脚本的句柄列为依赖项。

你使用“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');