【问题标题】:importing stylesheet with wp_enqueue_style() - wordpress not working使用 wp_enqueue_style() 导入样式表 - wordpress 无法正常工作
【发布时间】:2017-04-08 06:24:34
【问题描述】:

尝试导入自定义 CSS 样式表,但它没有显示在检查元素中并且无法正常工作。 style.css 位于根目录中名为 CSS 的文件夹中。有什么想法吗?

函数.php

<?php 
function learningWordPress_resources() {
        wp_enqueue_style('samirtheme-css', get_template_directory_uri().'/css/style.css', array(), '1.0.0', 'all');
        // wp_enqueue_style('customstyle', get_template_directory_uri().'/css/style.css', array(), '1.0.0', 'all');

}

add_action('wp_enqueue_scripts', 'learningWordPress_resources');

?>

header.php

<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width">
<title> <?php bloginfo('name') ?> </title>

<?php/* allows wordpress to add whatever it needs to */wp_head(); ?>
</head>

【问题讨论】:

  • view page source 上找不到它?代码看起来不错
  • CSS 甚至无法正常工作。也不显示在检查元素中
  • 你检查你的function.php 被调用了吗?尝试将一些日志放在那里并检查它是否有效,然后检查你的钩子是否被 WordPress 调用
  • 我可以在functions.php 中编写显示的html 元素。不知道你所说的日志是什么意思? wordpress 新手,正在学习。
  • 好的然后检查添加这一行die('my hooks called');作为learningWordPress_resources的第一行然后重新加载页面。如果你看到我的钩子叫做 msg,那么你的钩子正在工作。不要在生产站点上尝试这个。

标签: php css wordpress


【解决方案1】:

get_template_directory_uri() 返回当前父主题文件夹的网络路径。如果您不使用子主题,这也是您当前主题的文件夹。

但是,如果您不是创建将被其他人使用的主题的开发人员,您应该使用get_stylesheet_directory_uri(),它始终返回您当前主题文件夹的网络路径(无论它是否是子主题)。

当然,您应该将自定义样式表放入

/wp-content/themes/{current_theme}/css/

如果将样式表放入位于根目录下的/css 文件夹对您很重要,请使用get_site_url()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多