如何在functions.php(wordpress)中加载bootstrap脚本和样式? [英] How to load bootstrap script and style in functions.php (wordpress)?

查看:66
本文介绍了如何在functions.php(wordpress)中加载bootstrap脚本和样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I converted bootstrap theme to wordpress. Now, I have a problem in loading the bootstrap scripts and style. This is my code in 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' );

我将此添加到我的标题中

and i add this in my header

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

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

Additional question: What is the difference between wp_register and wp_enqueue? Please help me. Im beginner in using bootstrap.

推荐答案

您需要做的就是将它们排队。另外,我建议摆脱你的jquery导入(第二个wp_enqueue)。 WordPress默认包含jQuery,你已经将它包含在第一个脚本中(因为你已将它列为依赖项)。这是一个示例,但是这会将jquery排队两次(本机jquery和引导程序jquery):

All you need to do is enqueue them. Also, I recommend getting rid of your jquery import (the second wp_enqueue). WordPress includes jQuery by default, and you're already including it in the first script (because you have listed it as a dependency). Here's an example, but this enqueues jquery twice (the native jquery, and the 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' );

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

The only reason you'd need to register the scripts before enqueue-ing them, is if one of the scripts is a dependency. From the docs:


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

wp_register_script() registers a script file in WordPress to be linked to a page later using the wp_enqueue_script() function, which safely handles the script dependencies.

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

Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued. WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script's handle as a dependency.

这篇关于如何在functions.php(wordpress)中加载bootstrap脚本和样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆