0

So I am super new in wordpress theme development, and in web development overall and is trying to understand the WP ecosystem. Any help will be much appreciated

while going through few WP tutorials, I found out there are multiple ways to include style.css files in wordpress. Namely,

  1. <?php bloginfo('stylesheet_url'); ?>
  2. <?php echo get_stylesheet_uri(); ?>
  3. <?php echo get_template_directory_uri(); ?>/style.css

The top 3 lines of code can be added in header.php

Apart from that we can also use the function- wp_enqueue_style()

My question is, what is the correct way to include style.css in a wordpress templete..

2 Answers2

2

I believe using wp_enqueue_style() is basic and more appropriate.

If you wanted the stylesheet to only be referenced on a particular page template then you do this

if ( is_page_template('template-directory/template-name.php')) {
    wp_enqueue_style( 'theme_css', get_template_directory_uri() . '/directory/filename.css');
}

or you just do this if it is not specific to a page template.

wp_enqueue_style( 'theme_css', get_template_directory_uri() . '/directory/filename.css');

This piece of code will need to go in your functions.php file. Read more about it here -> https://developer.wordpress.org/themes/basics/including-css-javascript/#stylesheets

You also want to be careful about the order in which you have referenced your css files -> In which order do CSS stylesheets override?

Mark
  • 146
  • 7
  • Thanks Mark... That is helpfull – Amarta Dey Dec 31 '17 at 15:36
  • It's more efficient to load CSS files and JavaScript files with functions `wp_enqueue_style` and `wp_enqueue_script` then I upvote this answer. – mmm Dec 31 '17 at 15:41
  • If we take in consideration the [WordPress Coding Standards](https://codex.wordpress.org/WordPress_Coding_Standards) and [Best Practices](https://developer.wordpress.org/plugins/the-basics/best-practices/) this should be the accepted answer. – Mithc Dec 31 '17 at 15:41
0

All will work for you and there isn't something like "correct way" in my idea. anyway using <?php bloginfo('stylesheet_url'); ?> because it is wordpress function for this, and it is in head tag can be better.

elyas
  • 35
  • 5