簡體   English   中英

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

[英]How to include styles.css in wordpress theme?

我正在嘗試將我的 style.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 文件的最佳方法是在themes functions.php添加以下代碼:

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

最佳實踐是在functions.php創建一個自定義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' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM