G
G
Grisha Nikolsky2015-12-12 22:27:11
WordPress
Grisha Nikolsky, 2015-12-12 22:27:11

How to include CSS?

Hello. Using XAMPP, I'm building a test WordPress site. Before that, I used styles like this:

<link rel="stylesheet" type="text/css"  href="<?php echo bloginfo('template_url'); ?>/css/index.css">

But now, as an experienced user, he decided to write the following in functions.php:
function add_resources(){
    wp_enqueue_style('index', bloginfo('template_url') . '/css/index.css');
}
add_action('wp_enqueue_scripts','add_resources');

And now I have index.php with a tag <body>and a doctype. There is nothing else there. And a stylesheet that changes the background. The first way works great, the second doesn't. What to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mr Crabbz, 2015-12-12
@Punkie

Make sure before the closing </head> tag you have:
<?php wp_head(); ?>

I
Igor Vorotnev, 2015-12-19
@HeadOnFire

There is such a thing called "standards". So.
1. Styles must be at the root of the theme folder in the style.css file
2. This file must contain correct headers that define the theme (WP parses them)
3. The header.php template must contain the line wp_head();
4. Styles are connected via functions.php

function my_scripts_and_styles() {

  // Theme stylesheet.
  wp_enqueue_style( 'my-style', get_stylesheet_uri() );

  // Theme script.
  wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), NULL, true );
  // Аргумент array( 'jquery' ) позволяет вызвать его как зависимость, вместо того чтобы вручную подключать.
  // Если jQuery не нужен, передавайте пустой массив - array()

}
add_action( 'wp_enqueue_scripts', 'my_scripts_and_styles' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question