我为自己设计了一个WordPress主题.

为了向WordPress主题介绍style和脚本文件,我在function文件中包含了以下代码,以便可以向主题介绍我的CSSJS文件:

add_action( 'wp_enqueue_scripts', 'files_assets' );

function files_assets() {
    
// Introducing CSS files
  wp_enqueue_style( 'Bootstrap-min', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '4.0.0' );


// Introducing JS files
  wp_enqueue_script( 'jQuery3.4.1', get_template_directory_uri() . '/js/jquery-3.4.1.min.js', array( 'jquery' ), '1.4.1', true );

}

因为我的模板有很多stylingscripts,所以我将stylescript文件分成几个部分.

现在我有35 css个文件和26 script个文件,我需要通过上面的代码介绍到我的模板,这是非常困难的!

有没有一个code而不是介绍单个文件,而是我只能介绍所需的folder,并且该文件夹中的所有文件都将链接到我的WordPress模板?

或者,有没有一种更简单的方法,让我不必逐个将61 files介绍到我的模板中?

推荐答案

您可以通过使用类和对象来实现这一点.您需要为所有文件创建一个对象.

例如,我有一些文件,如test1.css、test2.css等.

试着这样做:

/**
 * Register styles
 */
class JSXStyle {
    /**
     * Construct function.
     *
     * @param FileName $name file name.
     */
    public function __construct( $name ) {
        $this->name = $name;
        add_action( 'wp_enqueue_scripts', array( $this, 'register_custom_style' ) );
    }

    /**
     * Function for registering custom files.
     */
    public function register_custom_style() {
        wp_enqueue_style( $this->name, get_stylesheet_directory_uri() . "/assets/css/{$this->name}.css", array(), '1.0.0' );
    }
}

new JSXStyle( 'test1' );
new JSXStyle( 'test2' );
new JSXStyle( 'test3' );
new JSXStyle( 'test4' );

/**
 * Register scripts
 */
class JSXScript {
    /**
     * Construct function.
     *
     * @param FileName $name file name.
     */
    public function __construct( $name ) {
        $this->name = $name;
        add_action( 'wp_enqueue_scripts', array( $this, 'register_custom_script' ) );
    }

    /**
     * Function for registering custom files.
     */
    public function register_custom_script() {
        wp_enqueue_script( $this->name, get_stylesheet_directory_uri() . "/assets/js/{$this->name}.js", array( 'jquery' ), '1.0.0', true );
    }
}

new JSXScript( 'test1' );
new JSXScript( 'test2' );
new JSXScript( 'test3' );
new JSXScript( 'test4' );

Php相关问答推荐

在不刷新页面的情况下无法使用JQuery更新id

如何在WordPress中为特定自定义帖子类型自定义URL struct

从WooCommerce Checkout Country Select2下拉列表中重新排序国家/地区

在保留唯一键值对的情况下高效合并嵌套数组

Laravel:从匹配的数组数据中获取记录

照明\Support\Collection::Filter():参数#1($Callback)的类型必须为?可调用、已给定数组、已调用

在Google云存储对象上从PHP设置缓存控制TTL会更改错误的元数据

MULTI UPDATE Laravel为什么只能更新我的第一行

如何将隐式乘法添加到PHPPEG基本计算器语法中?

将数组推送到数组时,数组推送()出现问题

如何将LARAVEL模块路径和项目路径合并,因为当我使用模块路径时,它们会覆盖项目路径

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

如何在其他数组php中创建具有关联数组的数组

invalid_grant 和无效的 JWT 签名

批量更新 WooCommerce 中所有产品的特定自定义字段

Woocommerce 中为空时隐藏显示变体自定义字段数据

PHP向API发送curl请求

simplexml_load_file 和 simplexml_load_string 返回具有不同编码的相同数据

PHP 中的curl -F request=@file.xml

PHP/Laravel 中的自定义时区