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

functions - CSS stylesheet not loading

programmeradmin3浏览0评论

I'm brand new at theme development. My css stylesheet is not loading, and I'm not sure if the problem is in the functions.php, style.css, index.php, header.php, or footer.php file. I've added a div class "post-title" in index.php, which should change the font color of my posts, but right now it's not doing that. My code is below:

functions.php:

            <?php
                function link_css_stylesheet() {
                    wp_enqueue_style('style', get_stylesheet_uri());
                }

                add_action('wp_enqueue_scripts', 'link_css_stylesheet');
            ?>

style.css:

            /*
            Theme Name: Richard Theme Name
            Theme URI: 
            Author: Richard
            Author URI: 
            Description: Theme project.  
            Version: 1.0
            */

            .post-title {
                color : rgb(0,100,0);
            }

index.php:

            <?php
                get_header();
                ?>

                <div class="post-title">

                    <?php

                    if (have_posts()) : 
                        while (have_posts()) : the_post();
                            the_title();
                            the_excerpt();
                        endwhile; 
                    else: 
                        echo "No posts.";
                    endif;

                    ?>

                </div>

                <?php
                get_footer();
            ?>

header.php:

            <!DOCTYPE html>
                <html>
                    <head>
                        <meta charset=”<?php bloginfo(‘charset’); ?>
                        <title><?php wp_title(); ?> | <?php bloginfo('name'); ?></title>
                        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
                        <?php wp_head(); ?>
                    </head>
                        <body <?php body_class(); ?>>
                            <h1><?php bloginfo(‘name’); ?></h1>
                            <h2><?php bloginfo(‘description’); ?></h2>

footer.php:

            <p>this is a footer</p>
            </body>
            </html>

Thank you in advance for any tips you might be able to offer.

I'm brand new at theme development. My css stylesheet is not loading, and I'm not sure if the problem is in the functions.php, style.css, index.php, header.php, or footer.php file. I've added a div class "post-title" in index.php, which should change the font color of my posts, but right now it's not doing that. My code is below:

functions.php:

            <?php
                function link_css_stylesheet() {
                    wp_enqueue_style('style', get_stylesheet_uri());
                }

                add_action('wp_enqueue_scripts', 'link_css_stylesheet');
            ?>

style.css:

            /*
            Theme Name: Richard Theme Name
            Theme URI: http://www.intechio/themes/Richard-Theme
            Author: Richard
            Author URI: https://intechio
            Description: Theme project.  
            Version: 1.0
            */

            .post-title {
                color : rgb(0,100,0);
            }

index.php:

            <?php
                get_header();
                ?>

                <div class="post-title">

                    <?php

                    if (have_posts()) : 
                        while (have_posts()) : the_post();
                            the_title();
                            the_excerpt();
                        endwhile; 
                    else: 
                        echo "No posts.";
                    endif;

                    ?>

                </div>

                <?php
                get_footer();
            ?>

header.php:

            <!DOCTYPE html>
                <html>
                    <head>
                        <meta charset=”<?php bloginfo(‘charset’); ?>
                        <title><?php wp_title(); ?> | <?php bloginfo('name'); ?></title>
                        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
                        <?php wp_head(); ?>
                    </head>
                        <body <?php body_class(); ?>>
                            <h1><?php bloginfo(‘name’); ?></h1>
                            <h2><?php bloginfo(‘description’); ?></h2>

footer.php:

            <p>this is a footer</p>
            </body>
            </html>

Thank you in advance for any tips you might be able to offer.

Share Improve this question edited Nov 4, 2017 at 21:22 dimery2006 asked Nov 4, 2017 at 16:12 dimery2006dimery2006 1411 gold badge1 silver badge5 bronze badges 1
  • Check the resulting HTML source to see what get_stylesheet_uri() return in functions.php. – Max Yudin Commented May 21, 2018 at 11:04
Add a comment  | 

5 Answers 5

Reset to default 2

I have tried to create WordPress theme using your functions.php, style.css and index.php and for me, the stylesheet is loading correctly. Please make sure you have added your header.php and footer.php for the theme. Please study some tutorials for theme development on WordPress.You can look at this link https://codex.wordpress/Theme_Development for detailed information.

It turns out there was nothing wrong with my code. The problem lies in the browser (Chrome) not refreshing the css styles. I initially thought the stylesheet was not loading, but in fact the browser was simply needing a refresh. The solution is to force a cache refresh by typing Ctrl + F5. Thanks to all of you for the help!

It's likely that your header.php file doesn't include:

<?php wp_head(); ?> 

This function will add the HTML markup to include your CSS file.

I have created a simple WordPress theme using your codes and i see that your style is working correctly.

But there is some syntax issue in header.php file.

 <meta charset="<?php bloginfo('charset'); ?>

You have missed a > to close meta tag and a " to close charset. Correct one is:

 <meta charset="<?php bloginfo('charset'); ?> ">

Also in order to avoid further issues, please check WordPress theme development documentations.

Just as a quick guides, You need to use wp_footer() function in footer.php.

Also avoid using ?> at the end of file. because any character after it will cause headers already sent error and you may see only a blank page.

wp_enqueue_style('style', get_stylesheet_uri());

Does not return style.css

It returns the theme folder.

Try something like this:

function link_css_stylesheet() {
    wp_enqueue_style('style', get_bloginfo('template_directory').'/style.css');
}
add_action('wp_head', 'link_css_stylesheet');
发布评论

评论列表(0)

  1. 暂无评论