每次我分析免费的wordpress。org主题,我无法找到css文件在主题中是如何链接的。据我所知,css文件应该链接到header.php
使用链接标记。但每次我检查主题时,我都没有看到任何通过链接标签链接css的代码。检查此主题的示例header.php
https://imgur.com/a/EwSe7. 我的问题是,如果文件没有显示在header.php
?
如何在不在header.php中调用的情况下链接CSS文件?
2 个回复
最合适的回答,由SO网友:Narek Zakarian 整理而成
在中注册样式的正确方法Wordpress
就是让他们通过wp_enqueue_style
在主题中的功能functions.php
.你可以阅读并学习如何做here - wp_enqueue_style
/**
* Proper way to enqueue scripts and styles
*/
function wpdocs_theme_name_scripts() {
wp_enqueue_style( \'style-name\', get_stylesheet_uri() );
wp_enqueue_script( \'script-name\', get_template_directory_uri() . \'/js/example.js\', array(), \'1.0.0\', true );
}
add_action( \'wp_enqueue_scripts\', \'wpdocs_theme_name_scripts\' );
SO网友:HU ist Sebastian
Wordpress使用一个过滤器和操作系统在特定的时间做特定的事情。其中一个操作是wp\\u enqueue\\u脚本,它在wp\\u head()函数中调用(您肯定应该在header.php中找到)。
可以使用命令add\\u action向此操作添加函数。Wordpress还具有添加名为wp\\u enqueue\\u style的样式表的功能。这是为了确保同一个样式表或javascript不会在同一个文档中多次链接。
所以,如果你研究你的主题功能。php,您肯定会发现如下内容:
function include_my_funky_styles() {
wp_enqueue_style( \'my-style-name\', get_stylesheet_uri() );
// this function says "put the stylesheet into the header"
}
add_action( \'wp_enqueue_scripts\', \'include_my_funky_styles\' );
//this function says "when doing the action enqueue_scripts within the header, also do this
快乐编码,Kuchenundkakao结束