Wordpress 中的函数 function.php

Functions in Wordpress function.php

我已经开始为 Wordpress 构建主题。添加某个点 我想给站点添加一些样式。我可以在将样式添加到页眉时让它工作,但我想使用 function.php 文件来完成此操作。这是我得到的:

<?php
function addCss(){
    wp_enqueue_style('style', get_template_directory_uri() . 'style.css');
}

add_action("wp_enqueue_scripts", "addCss");
?>

这是一个非常简单的函数,但由于某种原因,该函数没有被触发。我也没有收到任何错误,并且 style.css 未根据控制台添加到站点。

我的文件结构:

我试过寻找解决方案,但没有找到。希望大家帮帮我

get_template_directory_uri() return 末尾没有“/”的路径。

在您的路径中添加“/”:

// add this line to create a versionning of your stylesheet
$ver = time();
wp_enqueue_style('style', get_template_directory_uri() . '/style.css', $ver);

希望对你有帮助!

确保在结束 </head> 标记之前在 header.php 中调用 <?php wp_head(); ?>

function addMyStyle() {
    wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );

}
add_action('wp_head', 'addMyStyle');

您需要 wp_head() 在 header.php

 if ( ! function_exists( 'themescripts' ) ) {
    function themescripts() {
            wp_enqueue_style('style', get_stylesheet_uri(), array());

            wp_enqueue_script( 'jquery', array(), '', true);
            ....
        }

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

此外,请确保 footer.php 文件中包含 wp_footer()。