functions.php 未调用 Wordpress 排队功能;未添加样式表

Wordpress enqueue function is not being called from functions.php; stylesheet not added

我正在构建我的第一个 wordpress 主题,并试图将外部 style.css 文件加入队列。

该文件位于主题的主目录中。

我创建了一个 functions.php 文件并将以下代码添加到:

<?php

function addExternalStuff(){
    wp_enqueue_style('style', get_stylesheet_uri());

}  
add_action('wp_enqueue_scripts','addExternalStuff');  

这正是教程和文档所说的要添加的内容。

我也在 add_action() 中使用了 wp_enqueue_style。

<?php

function addExternalStuff(){
    wp_enqueue_style('style', get_stylesheet_uri());

}  
add_action('wp_enqueue_style','addExternalStuff'); 

没有任何反应。样式表未添加到代码中。我在函数的顶部也添加了一个 echo,但它没有被打印出来。我还在 functions.php 的最顶部添加了一个 echo,在 addExternalStuff() 函数之外并打印出来,这意味着这是更正 functions.php 文件。

经过一些挖掘,我了解到我没有在 header.php 文件的 header 中包含 wp_head()。这就是 Wordpress 将样式表添加到 header 并且没有它就无法入队的方式(也是 footer.php 中的 wp_footer)。

在header.php中:

<head>

     <?php wp_head() ?>

</head>

在 functions.php 中也使用了 get_template_directory_uri() 而不是 get_stylesheet_uri() 这样我就可以使用默认 style.css.[=17= 以外的唯一名称调用样式表]

  <?php

    function addExternalStuff(){
        wp_enqueue_style('style',get_template_directory_uri().'css/new_stylesheet.css');

    }  
    add_action('wp_enqueue_scripts','addExternalStuff');