0

I am totally new to WordPress and developing my own theme but function.php is not loading CSS file.

header.php

<html>
    <head>     
        <?php wp_head(); ?>            
    </head>
    <body>
        <h2>I'm header.php</h2>

single.php

<?php
get_header();
while (have_posts()) {
    the_post();
    ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>

<?php }

?>

function.php

<?php
function main_css() {
    wp_enqueue_style('style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'main_css' );

style.css

/* 
Theme Name: My Theme
Author: Tesla
Version: 1.0    
*/
body{
    color: green;
}
h1{
    color: pink;
}

I have tried many ways but it's not loading even disable the browser cookies, hard refresh, reinstall the Wordpress.

Whats wrong with my code?

  • I have added <link rel='stylesheet' id='my-theme' href='<?php echo get_template_directory_uri(); ?>/style.css' type='text/css' media='all' /> and its workd fine but I think this is not a proper way to inlcude the CSS and JS files. – Polly Tearesa May 14 '18 at 17:12
1

The theme functions file name must be called functions.php not function.php. This can cause your code to not implement? You must check carefully.

  • Thanks a lot, @Mr.Optical. It works. I have been searching the solution for 2 days. – Polly Tearesa May 14 '18 at 17:23
  • @PollyTearesa you’re welcome :) – Quang May 14 '18 at 17:49
0

As was mentioned by Mr. Optical and Mat, the name of your functions files needs to be functions.php.

I think it's great that you're looking to learn how to code a WordPress Theme. One thing I recommend for aspiring theme developers is downloading the Underscores Starter Theme which is maintained by the team at Automattic.

This will give you the foundation that you can use for your theme or it can serve as a reference to learn from.

I also recommend book marking the WordPress Theme Developer Handbook. There, you'll get valuable information on how to properly develop a WordPress Theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.