首页 > 解决方案 > WordPress “wp_register_style 调用不正确”?

问题描述

我有个问题。当我想将此代码放在我的第一个 Wordpress 模板中时,不起作用。我有一个用于引导程序的库,还有一个用于我的风格的库。为什么?下面是我的代码。谢谢 :D

function load_stylesheets() // funtion name
    {
     wp_register_style('style', get_template_directory_uri() . 'style.css', array(), false, 'all');//style.css
     wp_enqueue_style('style');

     wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), false, 'all');// this is for bootstrap.
     wp_enqueue_style('bootstrap');

}

// 添加动作

  add_action('wp_enqueue_scripts', 'load_stylesheets');

//我的首页.php

<div class="container">


    <div class="row">

            <div class="col">
                Left    
            </div>

            <div class="col">
                Right
            </div>

    </div>

//style.css

body{

background-color: red;
 }

标签: phpcsstemplateswordpress-theming

解决方案


也许我做错了,但我还不能发表评论,但是我想说尝试从 the 中取出可选参数,wp_register_style然后让它:

wp_register_style('my-style', get_template_directory_uri() . 'style.css');
wp_enqueue_style('my-style');

您可以在此处查看可选参数:https ://developer.wordpress.org/reference/functions/wp_register_style/

不要忘记将 enqueue 的名称更改为您的样式的名称...如果您更改了样式的名称

另外,只需确保因为 Bootstrap 样式表位于 css 目录中,style.css 是在同一目录中还是 style.css 在您的主模板目录中?如果它在主模板目录中,那么它很好,但如果它在 css 目录中,您需要确保添加/css/style.css到您的wp_register_style


推荐阅读