繁体   English   中英

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

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

我一直在尝试创建一个 WordPress 主题,但在 header.php 中链接到 style.css 似乎不起作用,header 只是没有出现。 使用了 30 多个代码,甚至是 WordPress 和出现类似错误的人提供的代码,但解决方案似乎已过时。

<link href="<?php bloginfo('stylesheet_url'); ?>" rel = "stylesheet">

这是我的 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">

您的主题style.csswp_head()函数默认包含在 WordPress 中。 在结束 HEAD 标记之前,添加此函数。

<?php wp_head(); ?>

如果您想添加其他样式表,请在您的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' );

你有没有尝试过:

<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(). '/style.css' ?>">

并将该文件(我通常将其放在 header.php 文件中)与 style.css 文件放在同一目录中?

这就是我所做的。

对我来说,这很好用:

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

你可以试试这个

 <?php echo get_stylesheet_uri(); ?> 

而不是这个

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

这里

确保你的 style.css 文件在你的主题目录中

使用以下代码

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

希望有帮助

将以下内容放在wp_head();

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

参考https://wordpress.stackexchange.com/a/220719更新 fuction.php 文件通过 Wordpress Dashboard > Appearance > Theme Editor 主题文件列在右边。

将以下内容添加到您的 function.php 文件中

wp_enqueue_style ('style-name', get_template_directory_uri().'/mystylefile.css');

刷新您的 wordpress 缓存并确保您将文件通过 FTP 传输到主题的根目录。

您可以在 header.php 文件中包含 css,如下所示:

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

只需将其包含在标题中,不要将 style.css 放在这里

<?php wp_enqueue_style( 'style', get_stylesheet_uri() ); ?>

如果您要添加 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