2017-07-15 96 views
1

我正在使用本網站的指南https://www.taniarascia.com/developing-a-wordpress-theme-from-scratch製作一個新的WordPress主題。將一個CSS文件加載到WordPress中的新主題

教程的作家說,使用<link href="" rel="stylesheet">

是不加載腳本的最正確的方法。

我想在function.php上使用wp_enqueue_style()add_action()加載.css文件。我的主題目錄中已經有三個文件,index.php,function.php和style.css,但我似乎無法使它工作。

下面是我用來加載.css文件中的代碼:

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

add_action('wp_enqueue_scripts', 'enqueue_my_styles'); 

?> 

回答

1

被命名爲functions.phpfunction.php你的函數的文件嗎?這應該是functions.php並且是根本。

您的代碼看起來正確,但實際上可能會縮短一點,如twentyfifteen Wordpress模板;

function twentyfifteen_scripts() { 
    // Load our main stylesheet. 
    wp_enqueue_style('twentyfifteen-style', get_stylesheet_uri()); 
} 
add_action('wp_enqueue_scripts', 'twentyfifteen_scripts'); 

您會使用get_template_directory_uri()來加載模板目錄中的自定義文件。

function my_custom_styles() {   
    wp_enqueue_style('my-custom-style', get_template_directory_uri() . '/custom-style.css'); 
} 
add_action('wp_enqueue_scripts', 'my_custom_styles'); 
+0

感謝您指出function.php的文件名應該是functions.php。這完全是一個新手的錯誤,我的壞!對不起,非常感謝你的延長課程。我在這裏呆了好幾天。 – adi

+0

沒問題。每個人都必須從某個地方開始,所以要堅持下去。 :) – mrbubbles

相關問題