如何在 wordpress 主题中包含 styles.css?

How to include styles.css in wordpress theme?

我正在尝试将我的 styles.css 样式表包含在我正在尝试开发的 wordpress 主题中(我的第一个)。问题:一个人将如何去包括它?我知道将以下代码放入我的 header.php 文件中是有效的:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">

但是我更愿意像这样通过 functions.php 包含它:

<?php
function firstTheme_style(){
    wp_enqueue_style( 'core', 'style.css', false ); 
}
add_action('wp_enqueue_scripts', 'firstTheme_style');
?>

然而这根本不起作用(当我从我的 header.phps 'head' 中删除该行时,我的样式没有看到?

以下方法包括style.css

方法 - 1

// add in your header.php
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>">

方法 - 2

// load css into the website's front-end
function mytheme_enqueue_style() {
    wp_enqueue_style( 'mytheme-style', get_stylesheet_uri() ); 
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_style' );

方法 - 3

// Add this code in your functions.php
function add_stylesheet_to_head() {
      echo "<link href='".get_stylesheet_uri()."' rel='stylesheet' type='text/css'>";
}

add_action( 'wp_head', 'add_stylesheet_to_head' );

包含 styles.css 文件的最佳方法是在主题 functions.php 中添加以下代码:

function themename_enqueue_style() {
    wp_enqueue_style( 'themename-style', get_stylesheet_uri() ); 
}
add_action( 'wp_enqueue_scripts', 'themename_enqueue_style' );

最佳做法是在 functions.php

中创建自定义函数
function custom_function(){}

之后尝试使用此 wordpress 函数 wp_enqueue_style 调用 css 文件安全

function custom_function(){
wp_enqueue_style ('any-name',get_template_directory_uri().'/css/style.css');}

并添加add_action功能

add_action( 'wp_enqueue_scripts', 'custom_function' );