【问题标题】:how to include a css file in a wordpress template如何在 wordpress 模板中包含 css 文件
【发布时间】:2014-02-15 18:00:14
【问题描述】:

我正在尝试在我的 wordpress 网站中为所有页面添加一个新样式表。我想通过包含相同的模板 style.css 来做到这一点。但我无法真正弄清楚 style.css 是如何包含在内的。我正在使用 2012 主题

我在主题目录中看到了 style.css。但它是如何被包括在内的?基本上来自哪个文件?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    它的代码在functions.php的第152行。

    wp_enqueue_style( 'twentytwelve-style', get_stylesheet_uri() );
    

    要包含您自己的 CSS 文件,请使用:

    wp_enqueue_style( 'my-stylesheet', get_stylesheet_directory_uri() . '/my-stylesheet.css' );
    

    这相当于 /wp-content/themes/twentytwelve/my-stylesheet.css

    尽管我鼓励您创建一个子主题,但代码位于二十二个脚本样式函数中。在这种情况下,您将在子主题的 functions.php 中输入以下内容

    function wpse_load_css() {
        wp_enqueue_style( 'my-stylesheet', get_stylesheet_directory_uri() . '/my-stylesheet.css' );
    }
    add_action( 'wp_enqueue_scripts', 'wpse_load_css' );
    

    【讨论】:

    • @Nathan——这正是我想要的。这太棒了!
    • 很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多