簡體   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