wordpress css ファイル追加 functions.php

下記のように functions.php に記載することによりCSSを読み込める

//==この関数名は自由に決めれる
function university_files() {
  wp_enqueue_style('font-awesome','//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css
');
  wp_enqueue_style('university_main_styles', get_stylesheet_uri());
}

//==
add_action('wp_enqueue_scripts', 'university_files');

add_actionとは

特定のアクションにフックする。つまり特定のアクションが実行されたときに、一緒に呼ばれると考えて良い。

定義

<?php add_action( $hook, $function_to_add, $priority, $accepted_args ); ?>

第1引数

フックされるアクション名

第2引数

フックする関数名 ここでは上記の

university_filesを呼び出している

wp_enqueue_scriptとは

srcが提供されている場合はスクリプトを登録し(上書きしません)、それをエンキューします。

エンキューとは、入れた順に取り出すこと。

定義

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>

$handle

ハンドルとして使う名前、自由に命名して良い.ユニークでないといけない。

$src

url ローカルは指定しない。 bootstrapやCDNで取得するアドレスのhttpをのぞいた部分から記載する。

下記だったら
http://example.com/wp-content/themes/my-theme/my-theme-script.js
このように記載する
//example.com/wp-content/themes/my-theme/my-theme-script.js

プロトコルの影響を受けないため

$deps

このスクリプトが依存する配列 このスクリプトより前に読み込まれるひつようがある。

毎回キャッシュを読み込む設定 第3引数にNULL 第4引数に microtime() を渡す。

  wp_enqueue_style('university_main_styles', get_stylesheet_uri(), NULL, microtime());