php 如何在我的 WordPress 主题中使用 wp_enqueue_style()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3472087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use wp_enqueue_style() in my WordPress theme?
提问by jessegavin
I am building out my first WordPress site for a client. I really want to use LESSfor CSS and found a WP plugin named WP-LESS.
我正在为客户建立我的第一个 WordPress 网站。我真的很想将LESS用于 CSS,并找到了一个名为WP-LESS的 WP 插件。
Now, I am total WordPress newb, but it appears that this plugin requires me to use a function called wp_enqueue_style()to tell WordPress to process the .less file.
现在,我完全是 WordPress 新手,但似乎这个插件需要我使用一个名为wp_enqueue_style()的函数来告诉 WordPress 处理 .less 文件。
I can't figure out where I use this function. I looked in my header.php file in my theme directory and I see this.
我不知道我在哪里使用这个功能。我查看了我的主题目录中的 header.php 文件,我看到了这一点。
<link rel="stylesheet" type="text/css" media="all"
href="<?php bloginfo( 'stylesheet_url' ); ?>" />
Am I supposed to replace this code with something like this?
我应该用这样的东西替换这段代码吗?
<?php wp_enqueue_style('mytheme',
get_bloginfo('template_directory').'/style.less',
array('blueprint'), '', 'screen, projection'); ?>
回答by EAMann
Not quite, but almost. What you want to do is place a function in functions.php
that queues your script.
不完全是,但差不多。你想要做的是在functions.php
你的脚本队列中放置一个函数。
So:
所以:
function addMyScript() {
wp_enqueue_style('mytheme', get_bloginfo('template_directory').'/style.less', array('blueprint'), '', 'screen, projection');
}
add_action('wp_head', 'addMyScript');
Then make sure you have do_action('wp_head');
in your header.php
file and it should work just fine.
然后确保您do_action('wp_head');
的header.php
文件中有它并且它应该可以正常工作。
回答by lime-cat
wp_enqueue_style usage inside of the theme or the plugin:
wp_enqueue_style 在主题或插件中的用法:
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a parent theme
wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a child theme
wp_enqueue_style( 'my-style', plugins_url( '/css/my-style.css', __FILE__ ), false, '1.0', 'all' ); // Inside a plugin
回答by Chris Bauer
Ran into this issue myself, and EAMann's tip almost worked. It might be the version of WordPress (3.4), although I'm not a php developer so I'm not sure, but I needed this below the function instead of what was provided:
我自己遇到了这个问题,EAMan 的提示几乎奏效了。它可能是 WordPress (3.4) 的版本,虽然我不是 php 开发人员所以我不确定,但我需要在函数下方而不是提供的内容:
add_action('wp', 'useLess');
回答by Mukesh Panchal
Add below function in your theme function.php and you get style and script.
在您的主题 function.php 中添加以下函数,您将获得样式和脚本。
<?php
if ( ! function_exists( 'add_script_style' ) ) {
function add_script_style() {
/* Register & Enqueue Styles. */
wp_register_style( 'my-style', get_template_directory_uri().'/css/my-style.css' );
wp_enqueue_style( 'my-style' );
/* Register & Enqueue scripts. */
wp_register_script( 'my-script', get_template_directory_uri().'/js/my-script.js' );
wp_enqueue_script( 'my-script');
}
}
add_action( 'wp_enqueue_scripts', 'add_script_style', 10 );
?>
回答by Junaid Siddique
If you enter this code correctly you must be see this function is exist or not in your index.php file wp_head(); & wp_footer(); if is not exist, you need to be add this function in your index file. you need to add this wp_head(); before haed tag, and another one add before body tag.
如果您正确输入此代码,您必须看到此函数是否存在于您的 index.php 文件 wp_head(); & wp_footer(); 如果不存在,则需要在索引文件中添加此函数。你需要添加这个 wp_head(); 在haed标签之前,另一个在body标签之前添加。
function load_style() {
wp_register_style( 'my_style', get_template_directory_uri(). '/css/my_style.css' );
wp_enqueue_style( 'my_style' );
}
// Register style sheet.
add_action( 'wp_enqueue_scripts', 'load_style' );
function load_style() {
wp_register_style('my_style', get_template_directory_uri().'/css/my_style.css');
wp_enqueue_style('my_style');
}
// 注册样式表。
add_action('wp_enqueue_scripts', 'load_style');
For more details
欲知更多详情