入队 CSS 函数 [英] Enqueue CSS Function

查看:21
本文介绍了入队 CSS 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我自己的 WordPress 主题并尝试将我的 css 加入到 functions.php 中

I am creating my own WordPress theme and trying to enqueue my css in functions.php

当我导航到 wp-admin/Themes 时,它说我的模板丢失了,我只能假设它与我的功能有关,但我看不到在哪里.

When I navigate to wp-admin/Themes it says my template is missing, I can only assume its todo with my function but I cannot see where.

我的 PHP 文件位于主题根文件夹中,而 css 位于其自己的文件夹中

My PHP files live in the themes root folder and the css is in its own folder

Functions.php

<?php

function LoadResources () {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css' ); 
}

add_action('wp_enqueue_style', 'LoadResources');

Header.php

<!doctype html>
<html>
<head>
    <meta charset="<?php bloginfo('charset') ?>">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">

    <title><?php the_title() ?></title>

    <?php wp_head(); ?>

</head>
<body <?php body_class(); ?>>

编辑

推荐答案

正确的钩子是 wp_enqueue_scripts,而不是 wp_enqueue_style.更改以下行

The correct hook is wp_enqueue_scripts, not wp_enqueue_style. Change the following line

add_action('wp_enqueue_style', 'LoadResources');

add_action('wp_enqueue_scripts', 'LoadResources');

另外,函数名应该是小写的

Also, function names should be lowercase letters

请注意,主题的主样式表必须进入根文件夹.它不能位于子文件夹中.Wordpress 会自动从那里加载主样式表

Please note, the main stylesheet of the theme has to go into the root folder. It cannot be in a subfolder. Wordpress will automatically load the main stylesheet from there

这篇关于入队 CSS 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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