如何在 PHP/WordPress 中正确排队样式和脚本 [英] how to properly enqueue styles and scripts in PHP/WordPress

查看:25
本文介绍了如何在 PHP/WordPress 中正确排队样式和脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WordPress/PHP 的新手,我正在尝试创建一个主题,在教程中它说使用 wp_enqueue_style/script 来加载 css/js 文件但是由于某种原因我似乎无法获得 Bootstrap/JQuery 工作,这是我在functions.php 中的代码:

I'm new to WordPress/PHP and I'm trying to create a theme, during a tutorial it's saying to use the wp_enqueue_style/script to load css/js files however for some reason I can't seem to get Bootstrap/JQuery to work, this is my code in functions.php:

function rixcy_scripts() {
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css', array(), '' );
    wp_enqueue_style( 'blog', get_template_directory_uri() . '/css/blog.css' );
    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.js', array('jquery'), true );
}

add_action( 'wp_enqueue_scripts', 'rixcy_scripts' );

我还想添加我自己的自定义 javascript 文件 (blog.js),但我不确定如何执行此操作.

I'm also wanting to add my own custom javascript file (blog.js) but I'm unsure on how to do this.

任何帮助将不胜感激!

推荐答案

是的,您在 WordPress 中添加 css 和 js 的代码正确,但您需要提供唯一名称到每个 css 和 js

Yes your code for adding css and js in WordPress is correct but you need to give Unique Name to each css and js

试试下面的代码:

function rixcy_scripts() {
    wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/css/bootstrap.css', array(), '' );
    wp_enqueue_style( 'blog', get_template_directory_uri() . '/css/blog.css' );
    wp_enqueue_script( 'bootstrap-jquery', get_template_directory_uri() . '/js/bootstrap.js', array('jquery'), true );
    wp_enqueue_script( 'blog-js', get_template_directory_uri() . '/js/blog.js', array('jquery'), true );

}

add_action( 'wp_enqueue_scripts', 'rixcy_scripts' );

这篇关于如何在 PHP/WordPress 中正确排队样式和脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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