最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - wp_head() not inserting the default stylesheet style.css

programmeradmin3浏览0评论

I have a custom theme with just a name in its style.css. I've activated it in admin which shows it just fine.

In the index.php just <?php wp_head(); ?> and it doesn't output the line that is supposed to include my main stylesheet style.css:

<link rel='stylesheet' id='my-theme'  href='~/wp-content/themes/my-theme/style.css' type='text/css' media='all' />

It prints out all the other stuff but that line! What would make it not print out my default stylesheet inclusion line?


style.css :

/*
Theme Name: my-theme
*/

index.php :

hello world
<?php wp_head(); ?>

output:

hello world
<meta name='robots' content='noindex,follow' />
<link rel='stylesheet' id='open-sans-css'  href='//fonts.googleapis/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&#038;subset=latin%2Clatin-ext&#038;ver=4.0' type='text/css' media='all' />
<link rel='stylesheet' id='dashicons-css'  href='~/wp-includes/css/dashicons.min.css?ver=4.0' type='text/css' media='all' />
<link rel='stylesheet' id='admin-bar-css'  href='~/wp-includes/css/admin-bar.min.css?ver=4.0' type='text/css' media='all' />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="~/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="~/wp-includes/wlwmanifest.xml" /> 
<meta name="generator" content="WordPress 4.0" />
<style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen">
    html { margin-top: 32px !important; }
    * html body { margin-top: 32px !important; }
    @media screen and ( max-width: 782px ) {
        html { margin-top: 46px !important; }
        * html body { margin-top: 46px !important; }
    }
</style>

I have a custom theme with just a name in its style.css. I've activated it in admin which shows it just fine.

In the index.php just <?php wp_head(); ?> and it doesn't output the line that is supposed to include my main stylesheet style.css:

<link rel='stylesheet' id='my-theme'  href='~/wp-content/themes/my-theme/style.css' type='text/css' media='all' />

It prints out all the other stuff but that line! What would make it not print out my default stylesheet inclusion line?


style.css :

/*
Theme Name: my-theme
*/

index.php :

hello world
<?php wp_head(); ?>

output:

hello world
<meta name='robots' content='noindex,follow' />
<link rel='stylesheet' id='open-sans-css'  href='//fonts.googleapis/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&#038;subset=latin%2Clatin-ext&#038;ver=4.0' type='text/css' media='all' />
<link rel='stylesheet' id='dashicons-css'  href='~/wp-includes/css/dashicons.min.css?ver=4.0' type='text/css' media='all' />
<link rel='stylesheet' id='admin-bar-css'  href='~/wp-includes/css/admin-bar.min.css?ver=4.0' type='text/css' media='all' />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="~/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="~/wp-includes/wlwmanifest.xml" /> 
<meta name="generator" content="WordPress 4.0" />
<style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen">
    html { margin-top: 32px !important; }
    * html body { margin-top: 32px !important; }
    @media screen and ( max-width: 782px ) {
        html { margin-top: 46px !important; }
        * html body { margin-top: 46px !important; }
    }
</style>
Share Improve this question edited Sep 26, 2014 at 13:17 laggingreflex asked Sep 26, 2014 at 13:12 laggingreflexlaggingreflex 1,0303 gold badges14 silver badges23 bronze badges 2
  • You should not add these in index.php but these must be already in header.php and <?php wp_head(); ?> must be first line in index.php or all other theme files. – Robert hue Commented Sep 26, 2014 at 13:20
  • try adding <?php wp_head(); ?> in your header.php <head> section. – Mark P Commented Oct 3, 2019 at 22:48
Add a comment  | 

2 Answers 2

Reset to default 5

Actually you shouldn't add JS and CSS files to your header.php, but make use of the functions wp_enqueue_script() and wp_enqueue_style() to add them there.

Example taken from the codex page:

/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

You should add your .js and .css files, like this. in your header.php

<link rel='stylesheet' id='my-theme' href='<?php echo get_template_directory_uri(); ?>/style.css' type='text/css' media='all' />

Did you notice <?php echo get_template_directory_uri(); ?> in above code that you will need to use. It defines URL to theme folder on your website. You can add all other files links similarly.

Although it's not necessary to include /theme/style.css in your website unless it has any styles for website to use. I think your theme use this style.css file only for information/define theme variable purposes.

发布评论

评论列表(0)

  1. 暂无评论