How to include styles.css in wordpress theme?

扶醉桌前 提交于 2019-11-30 19:58:41

问题


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



来源:https://stackoverflow.com/questions/37391400/how-to-include-styles-css-in-wordpress-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!