【问题标题】:Enqueuing External JS and CSS in Wordpress在 Wordpress 中排队外部 JS 和 CSS
【发布时间】:2019-08-29 09:31:04
【问题描述】:

我的新网站有点困难。

确实有2个问题。

首先是调用外部 JS 和 CSS 脚本。我已将它们添加到我的 functions.php 文件中,方式与我的所有其他网站相同,但它们似乎没有加载

另一个是我的子主题CSS。它似乎加载在主页上,但在我的自定义页面(到目前为止只做了一个)上它没有加载。我在检查元素时检查了源选项卡,但它没有显示。

来自 functons.php 的外部脚本和 CSS 代码:

function external_load_scripts() {
    wp_register_script( 'popper', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js' array('jquery'),'1.14.7', true );
    wp_enqueue_script( 'popper' );
    wp_register_script( 'bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js' array('jquery'),'4.3.1', true );
    wp_enqueue_script( 'bootstrap' );
    wp_register_script( 'datatables', 'https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js' array('jquery'),'1.10.12', true );
    wp_enqueue_script( 'datatables' );
    wp_register_script( 'chosen', 'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js' array('jquery'),'1.4.2', true );
    wp_enqueue_script( 'chosen' );
    wp_register_script( 'select', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js' array('jquery'),'4.0.0', true );
    wp_enqueue_script( 'select' );
}

add_action('wp_enqueue_scripts', 'external_load_scripts');

function external_load_styles() {
    wp_register_style( 'chosencss', 'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css' );
    wp_enqueue_style( 'chosencss' );
    wp_register_style( 'selectcss', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css' );
    wp_enqueue_style( 'selectcss' );
    wp_register_style( 'bootstrapcss', 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' );
    wp_enqueue_style( 'bootstrapcss' );
    wp_register_style( 'allcss', 'https://use.fontawesome.com/releases/v5.0.13/css/all.css' );
    wp_enqueue_style( 'allcss' );
    wp_register_style( 'datatablescss', 'https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css' );
    wp_enqueue_style( 'datatablescss' );
}

add_action('wp_enqueue_scripts', 'external_load_styles');

在functions.php中注册子主题style.css

add_action( 'wp_enqueue_scripts', 'Eazy_Enqueue_styles' );
function Eazy_Enqueue_styles() {

    $parent_style = 'minimal-blocks-style'; 

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
} 

如前所述,子 style.css 似乎可以在主页上使用,但不适用于我使用自定义模板设计的自定义页面

解决这个问题的任何帮助都会很棒。提前致谢

【问题讨论】:

    标签: javascript jquery css wordpress enqueue


    【解决方案1】:

    还有一个我认为不好的解决方案

    您尝试将完整链接 css 和 js 粘贴到主题文件夹中的自定义页面中

    例如。 www.site.com/demo

    文件:demo-page.php

    【讨论】:

    • 您好,感谢您的回复。我不确定我是否理解。您认为我可以对此做出改进吗?我的 functions.php 文件保存在我的子主题文件夹中
    猜你喜欢
    • 2012-11-07
    • 2015-06-12
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2021-06-22
    • 2013-03-12
    • 2016-09-04
    相关资源
    最近更新 更多