简体   繁体   English

WordPress header.php中如何正确链接到CSS?

[英]How to link to CSS properly in WordPress header.php?

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.我一直在尝试创建一个 WordPress 主题,但在 header.php 中链接到 style.css 似乎不起作用,header 只是没有出现。 Used more than 30 codes even the ones provided by WordPress and people with similar errors but it seems like the solutions are outdated.使用了 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">

Your theme style.css is included by default by the WordPress by wp_head() function.您的主题style.csswp_head()函数默认包含在 WordPress 中。 Before you end your HEAD tag add this function.在结束 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' );

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.这就是我所做的。

对我来说,这很好用:

<link rel="stylesheet" type="text/css" href="<?php bloginfo("template_directory"); ?>/style.css" />

you can try this one你可以试试这个

 <?php echo get_stylesheet_uri(); ?> 

instead of this而不是这个

<?php bloginfo('stylesheet_url'); ?>

from here这里

make sure your style.css file is in your theme's directory确保你的 style.css 文件在你的主题目录中

Use the following code使用以下代码

<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />

Hope that helps希望有帮助

将以下内容放在wp_head();

<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />

refer to https://wordpress.stackexchange.com/a/220719 update 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');

flush your word press cache and make sure you FTP the file up to the root of the theme.刷新您的 wordpress 缓存并确保您将文件通过 FTP 传输到主题的根目录。

You can include css in header.php file as follow:您可以在 header.php 文件中包含 css,如下所示:

<link rel="stylesheet" href="<?php echo bloginfo('template_url').'/custom.css'; ?>">

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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM