php 如何将多个 CSS 文件与 WordPress 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18093124/
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 link multiple CSS files with WordPress
提问by Ryman Holmes
I know that to link your WordPress main style.css
file you use:
我知道要链接您使用的 WordPress 主style.css
文件:
<link href="<?php bloginfo('stylesheet_url');?>"rel="stylesheet" type="text/css"/>
However I have quite a few CSS files that need to be linked to the main PHP file for things like sliders, picture boxes etc...
但是,我有很多 CSS 文件需要链接到主要的 PHP 文件,以用于滑块、图片框等...
I'm not quite sure how I would do that because the <?php bloginfo('stylesheet_url');?>
only works for the stylesheet that is named styles.css
and my other stylesheets all have different names.
我不太确定我将如何做到这一点,因为<?php bloginfo('stylesheet_url');?>
唯一适用于已命名的样式表,styles.css
而我的其他样式表都有不同的名称。
Does anyone know how I can ink them all in?
有谁知道我怎样才能把它们全部印上?
回答by Jay
Just put all of your stylesheets in a directory wp-content\themes\twentyeleven\css
.Then you can link all these by simply put below code-
只需将所有样式表放在一个目录中wp-content\themes\twentyeleven\css
。然后您可以通过简单地将所有这些链接放在下面的代码中 -
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/css/style1.css" />
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/css/style2.css" />
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/css/style3.css" />
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/css/style4.css" />
enjoy coding.
享受编码。
回答by Ben Racicot
Your source code doesn't include a filename... bloginfo('stylesheet_url') only returns the link to your stylesheet...folder URL, usually the theme folder. You need to also append the folder (if one) and the filename.css
您的源代码不包含文件名... bloginfo('stylesheet_url') 仅返回指向您的样式表的链接...文件夹 URL,通常是主题文件夹。您还需要附加文件夹(如果有)和 filename.css
Remember to always code with WordPress standards in mind. Linking to a stylesheet is not a best practice. This enables proper caching and efficiency and is easier in the long run.
请记住始终牢记 WordPress 标准进行编码。链接到样式表不是最佳实践。这可以实现适当的缓存和效率,并且从长远来看更容易。
From the free 300 page book I read last weekend - WordPress AJAX, pg 53:
来自我上周末阅读的免费 300 页书 - WordPress AJAX,第 53 页:
// load styles + conditionally load an IE 7 stylesheet
add_action('init', 'my_theme_register_styles');
function my_theme_register_styles() {
//Register styles for later use
wp_register_style('my_theme_style1', get_stylesheet_directory_uri() . '/style1.css', array(), '1.0', 'all');
wp_register_style('my_theme_style2', get_stylesheet_directory_uri() . '/style2.css', array('my_theme_style1'), '1.0', 'all');
wp_register_style('my_theme_style3', get_stylesheet_directory_uri() . '/style3.css', array('my_theme_style1', 'my_theme_style2'), '1.0', 'all');
global $wp_styles;
$wp_styles->add_data( 'my_theme_style3', 'conditional', 'lte IE 7' );
}
Put this in your functions.php or your header.php. It properly conditionally loads a stylesheet for the IE's...
把它放在你的functions.php 或header.php 中。它有条件地为 IE 正确加载样式表...
回答by lee
Are the CSS file in the current theme's folder? If so, then try this code:
CSS 文件是否在当前主题的文件夹中?如果是这样,请尝试以下代码:
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/what-ever.css" />
It works for me.
这个对我有用。
回答by Ian
Probably the easiest way to add a style to your theme page if you are going to hardcode it would be: 1) Add your stylesheet to your stylsheet directory. 2) Paste in this code into your head (replacing style2.css with whatever you stylesheet name is).
如果您要对其进行硬编码,那么向主题页面添加样式的最简单方法可能是: 1) 将样式表添加到样式表目录中。2) 将此代码粘贴到您的头脑中(用您的样式表名称替换 style2.css)。
<link href="<?php echo get_stylesheet_directory_uri().'/style2.css'; ?>" rel="stylesheet" />
or
或者
<link href="<?php blog_info('template_url').'/style2.css'; ?>" rel="stylesheet" />
If your styles are in a separate folder, just make sure to append that folder to your path (ie. /styles/style2.css)
如果您的样式位于单独的文件夹中,请确保将该文件夹附加到您的路径中(即。/styles/style2.css)
Edit: Made answer more specific to add style links to the head and fixed my dumb mistake of src= when it should be href=
编辑:使答案更具体,以将样式链接添加到头部并修复我的 src= 愚蠢错误,当它应该是 href= 时