将样式表添加到入门主题

Add stylesheet to the starter theme

将样式表添加到 WordPress 的 Timber 入门主题的正确方法是什么?

我通常会在 functions.php 中排队我的样式表。但这是正确的方法吗?

您可以在 functions.php 中添加样式表(Wordpress 传统方式)或使用允许您将样式表直接添加到 twig 模板中的自定义函数(将在 functions.php 中添加) .这样您就可以仅在实际使用样式表的地方排队。

Timber 入门主题在 functions.php 文件中有一个自定义功能的特定部分。

要添加到 functions.php 的函数:

/** This is where you can add your own functions to twig.
     *
     * @param string $twig get extension.
     */
     $function = new Twig_SimpleFunction('enqueue_style', function ($handle, $src) {
        wp_enqueue_style( $handle, get_stylesheet_directory_uri() . '/static/css/'.$src);
     });
     $twig->addFunction($function);

更改 /static/css/ 以满足您的需要。现在您可以像这样将样式直接排入您的树枝模板中:

{{ enqueue_style('global','global.css') }} 

如果您需要添加外部样式表,您可以使用稍微不同的函数:

/** This is where you can add your own functions to twig.
     *
     * @param string $twig get extension.
     */
$function = new Twig_SimpleFunction('enqueue_style_ext', function ($handle, $src) {
    wp_enqueue_style( $handle, $src);
});
$twig->addFunction($function);

然后像这样入队:

{{ enqueue_style_ext('tachyons','https://cdnjs.cloudflare.com/ajax/libs/tachyons/4.11.1/tachyons.min.css') }}

原函数发在一个木头github issue