如何在 WordPress header.php 中正确链接到 CSS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25196920/
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 to CSS properly in WordPress header.php?
提问by Samu?l
I have been trying to create a WordPress theme but linking to style.css within header.php doesn't seems to work, the header just doesn't appear. Used more than 30 codes even the ones provided by WordPress and people with similar errors but it seems like the solutions are outdated.
我一直在尝试创建一个 WordPress 主题,但在 header.php 中链接到 style.css 似乎不起作用,标题只是没有出现。使用了 30 多个代码,即使是 WordPress 提供的代码和有类似错误的人,但似乎解决方案已经过时了。
<link href="<?php bloginfo('stylesheet_url'); ?>" rel = "stylesheet">
This is my PHP script
这是我的 PHP 脚本
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-default navbar-static-top">
<div class="container"> <a href="#" class="navbar-brand">Logo</a>
<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse"> <span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#">Home</a>
</li>
<li><a href="#">Contact</a>
</li>
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Social Media<b class = "caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Facebook</a>
</li>
<li><a href="#">Instagram</a>
</li>
<li><a href="#">Twitter</a>
</li>
<li><a href="#">Google Plus</a>
</li>
</ul>
</li> <a href="http://www.adds.com" class="navbar-btn btn-success btn pull-right">Add</a>
</ul>
</div>
</div>
</div>
<div class="container">
回答by dipak_pusti
Your theme style.css
is included by default by the WordPress by wp_head()
function. Before you end your HEAD tag add this function.
style.css
默认情况下,WordPress 按wp_head()
功能包含您的主题。在结束 HEAD 标记之前,添加此函数。
<?php wp_head(); ?>
If you want to add additional stylesheets then use this function in your functions.php
file.
如果要添加其他样式表,请在functions.php
文件中使用此函数。
function additional_custom_styles() {
/*Enqueue The Styles*/
wp_enqueue_style( 'uniquestylesheetid', get_template_directory_uri() . '/css/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'additional_custom_styles' );
回答by dipak_pusti
Have you tried:
你有没有尝试过:
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(). '/style.css' ?>">
And have that file (I usually place it in a header.php file) in the same directory as the style.css file?
并将该文件(我通常将其放在 header.php 文件中)与 style.css 文件放在同一目录中?
This is what I do.
这就是我所做的。
回答by Nico Martin
For me this works great:
对我来说,这很好用:
<link rel="stylesheet" type="text/css" href="<?php bloginfo("template_directory"); ?>/style.css" />
回答by Shah Rukh
回答by Brad Allen
Put the following underwp_head();
将以下内容放在wp_head();
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />
回答by s.lenders
Use the following code
使用以下代码
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />
Hope that helps
希望有帮助
回答by AtLeastTheresToast
refer to https://wordpress.stackexchange.com/a/220719update the fuction.php file through the Wordpress Dashboard > Appearance > Theme Editor theme files are listed on the right.
参考https://wordpress.stackexchange.com/a/220719更新 fuction.php 文件通过 Wordpress Dashboard > Appearance > Theme Editor 主题文件列在右边。
add the following to your function.php file
将以下内容添加到您的 function.php 文件中
wp_enqueue_style ('style-name', get_template_directory_uri().'/mystylefile.css');
wp_enqueue_style ('style-name', get_template_directory_uri().'/mystylefile.css');
flush your word press cache and make sure you FTP the file up to the root of the theme.
刷新您的 wordpress 缓存并确保您将文件通过 FTP 传输到主题的根目录。
回答by kulbhushan
Just include this in the header, don't put style.css here
只需将其包含在标题中,不要将 style.css 放在这里
<?php wp_enqueue_style( 'style', get_stylesheet_uri() ); ?>
if you are adding Bootstrap CDN then use that like this
如果您要添加 Bootstrap CDN,请像这样使用
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">