【问题标题】:Loading a CSS file to a new theme in WordPress将 CSS 文件加载到 WordPress 中的新主题
【发布时间】:2017-07-15 12:24:11
【问题描述】:

我正在使用来自本网站https://www.taniarascia.com/developing-a-wordpress-theme-from-scratch 的指南为 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' );

?>

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    您的函数文件是名为functions.php 还是function.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' );
    

    【讨论】:

    • 感谢您指出function.php的文件名应该是functions.php。这完全是新手的错误,我的错!很抱歉,非常感谢您延长课程。我被困在这里好几天了。
    • 没问题。每个人都必须从某个地方开始,所以要坚持下去。 :)
    猜你喜欢
    • 2020-09-21
    • 2017-11-18
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2013-04-28
    • 2013-09-28
    • 2017-06-15
    • 1970-01-01
    相关资源
    最近更新 更多