How to include styles.css in wordpress theme?

2019-02-16 00:52发布

问题:

I am trying to include my styles.css stylesheet in a wordpress theme I am trying to develop (my first one). Question: How would one go about including it? I know putting the following code in my header.php file works:

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

however I would rather like to include it through functions.php like so:

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

yet this doew not work at all (When i remove the line from my header.phps 'head', my styles are not seen?

回答1:

Following method are include style.css.

Method - 1

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

Method - 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' );

Method - 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' );


回答2:

The best way to include your styles.css file is to add the following code in your themes functions.php

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