【问题标题】:Dashes in functions.phpfunctions.php 中的破折号
【发布时间】:2019-02-12 04:20:59
【问题描述】:

WordPress 5.0.3

我正在阅读我的第一本关于 WordPress 的书。

我只是在为现有的 27 主题创建一个子主题。

书中说我要创建functions.php并粘贴:

add_action( 'wp_enqueue_scripts', 'wpquickstart_enqueue_styles' ); 
function wpquickstart_enqueue_styles() { 
    wp_enqueue_style( 'twenty-seventeen-style', get_template_directory_uri() . '/style.css' ); 
    wp_enqueue_style( 'wpquickstart-style',    get_stylesheet_directory_uri() . '/style.css', 
        array('twenty-seventeen-style')    ); 
}

好吧,被陷害在其中是可行的,这对我来说很奇怪。 困扰我的是二十七式的第一次冲刺。

我的意思是这也有效(注意二十七式,其中的破折号):

<?php
add_action( 'wp_enqueue_scripts', 'wpquickstart_enqueue_styles' ); 
function wpquickstart_enqueue_styles() { 
    wp_enqueue_style( 'twentyseventeen-style', get_template_directory_uri() . '/style.css' ); 
    wp_enqueue_style( 'wpquickstart-style',    get_stylesheet_directory_uri() . '/style.css', 
        array('twentyseventeen-style')    ); 
}
?>

能否告诉我破折号是否被忽略?我在哪里可以阅读有关这一刻的一些文档?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    由于您将第二个参数传递给wp_enqueue_style(),因此它将ha dle 定义为source。如果您之前使用wp_register_style() 注册了句柄和源,那么您不需要将源参数传递给wp_enqueue_style(),只要您以相同的方式调用句柄即可。

    否则,由于您同时定义句柄和源,它实际上是任意的。但是,如果您需要对入队样式进行出列、修改、添加内联样式等 - 您将使用您在此处传递的句柄(带或不带破折号)来实现。

    编辑:稍微简化一下:

    当你写作时:

    wp_enqueue_style( 'twentyseventeen-style', get_template_directory_uri() . '/style.css' );
    

    Michael,您正在添加一个名为 twentyseventeen-style 的样式表,该样式表位于 /template/style.css

    你可以随意命名它,它仍然会加载。例如,这也可以:

    wp_enqueue_style( 'blahblah-michaels-2k17-style', get_template_directory_uri() . '/style.css' );
    

    第一个参数是您给位于第二个参数位置的样式表的名称。

    【讨论】:

    • 说实话,你说的我什么都听不懂。我已经提到,我只是一个初学者。这是我生命中的第一个functions.php。能不能写的简单一点?困扰我的是 wp_enqueue_style( 'twenty-7teen-style', get_template_directory_uri() . '/style.css' );我可以看到这个字符串获得了所提到主题的模板目录。为什么它可以提到有或没有破折号?
    • 我已经澄清了一点。您可能还需要阅读 wp_enqueue_style() 的源文档:developer.wordpress.org/reference/functions/wp_enqueue_style
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2023-03-20
    • 2011-11-05
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多