以下のコードをテーマに書く必要はありませんので、削除しましょう。
html
1 < script src = " http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js " > </ script >
WordPress の場合、JavaScript や CSS を読み込む際、テーマの header.php や footer.php に script タグや link タグは記述しません。以下にシンプルな header.php と footer.php の例を示します。
header.php の例
php
1 <! DOCTYPE html >
2 < html <?php language_attributes ( ) ; ?> class = " no-js " >
3 < head >
4 < meta charset = " <?php bloginfo ( 'charset' ) ; ?> " >
5 < meta name = " viewport " content = " width=device-width, initial-scale=1 " >
6 < link rel = " profile " href = " http://gmpg.org/xfn/11 " >
7 <?php if ( is_singular ( ) && pings_open ( get_queried_object ( ) ) ) : ?>
8 < link rel = " pingback " href = " <?php bloginfo ( 'pingback_url' ) ; ?> " >
9 <?php endif ; ?>
10 <?php wp_head ( ) ; ?>
11 </ head >
12
13 < body <?php body_class ( ) ; ?> >
footer.php の例
php
1 <?php wp_footer ( ) ; ?>
2 </ body >
3 </ html >
CSS や JavaScript の読み込み方
テーマの functions.php に以下のコードを追加してください。
※ teratail60093 の部分は接頭辞(プレフィックス)です。
※ teratail60093 の部分は、テーマ名などに変更してください。
※ WordPress で関数やクラスを作成する時は、接頭辞を付けるのがマナーになっています。
php
1 function teratail60093_scripts ( ) { }
2 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
次に、テーマの CSS ファイルを読み込むために、teratail60093_scripts() 関数内に以下のように追記してください。
※ teratail60093 の部分は先程と同じく適切書き換えてください。
php
1 function teratail60093_scripts ( ) {
2 // Theme stylesheet.
3 wp_enqueue_style ( 'teratail60093-style' , get_stylesheet_uri ( ) ) ;
4 }
5 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
これで、テーマフォルダー直下の style.css が head タグ内に出力されるようになります。仮に「テーマフォルダー直下の style.css はテーマ名だけ記述して空なんだよねー。css フォルダ内の style.css を読み込ませたいよー」という場合は、以下のようにします。
※ teratail60093 の部分は先程と同じく適切書き換えてください。
php
1 function teratail60093_scripts ( ) {
2 // Theme stylesheet.
3 wp_enqueue_style ( 'teratail60093-style' , get_stylesheet_directory_uri ( ) . '/css/style.css' ) ;
4 }
5 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
これで、テーマーフォルダ/css/style.css が head タグ内に出力されるようになります。
これまでは、CSS ファイルの読み込み方を説明してみましたが、JS ファイルの場合は、wp_enqueue_style() 関数の部分を wp_enqueue_script() 関数に変更します。wp_enqueue_style() 関数も wp_enqueue_script() 関数も使い方は一緒です。詳しくは、以下ドキュメントを読んでみてください。
関数リファレンス/wp enqueue style - WordPress Codex 日本語版
関数リファレンス/wp enqueue script - WordPress Codex 日本語版
以下に、wp_enqueue_script() 関数で JS ファイルを読み込む簡単な例を示します。
php
1 function teratail60093_scripts ( ) {
2 // Theme script.
3 wp_enqueue_script ( 'teratail60093-script' , get_stylesheet_directory_uri ( ) . '/js/script.js' ) ;
4 }
5 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
CSS や JavaScript の依存関係
CSS ファイルや JS ファイルを読み込む時、以下のようなことが往々にしてしてあると思います。
CSS フレームワーク(Bootdtrap や Foundation)を読み込んだ後に、自身の CSS ファイルを読み込みたい!
InternetExplorer(IE)用にレイアウトを調整した CSS ファイルをメインの CSS ファイルの後に読み込みたい!
jQuery の後に jQuery プラグインを読み込んで、その後に自身の JS ファイルを読み込みたい!
このような場合でも、先程の「CSS や JavaScript の読み込み方」を少し変更するだけで実装できます。以下に CSS フレームワークの Bootstrap の後に自身の CSS ファイルを読み込む例を示します。
php
1 function teratail60093_scripts ( ) {
2 // CSS framework.
3 wp_enqueue_style ( 'bootstrap' , get_stylesheet_directory_uri ( ) . '/css/bootstrap.min.css' ) ;
4 // Theme stylesheet.
5 wp_enqueue_style ( 'teratail60093-style' , get_stylesheet_directory_uri ( ) . '/css/style.css' , array ( 'bootstrap' ) ) ;
6 }
7 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
注目していただきたいのは、テーマの CSS ファイルを読み込んでいる wp_enqueue_style() 関数の使用方法です。先程までは指定していなかった第三引数を指定しています。第三引数の部分は、この CSS ファイルより前に読み込まれる必要がある CSS ファイルのハンドルを配列で指定します。
※ ハンドル = wp_enqueue_style() 関数や wp_enqueue_script() 関数の第一引数に指定されている値
これは、wp_enqueue_script() 関数でも同じです。詳しくは、以下ドキュメントを読んでみてください。
関数リファレンス/wp enqueue style - WordPress Codex 日本語版
関数リファレンス/wp enqueue script - WordPress Codex 日本語版
今回の質問の解決策
長い前置き(偉そうな説明)がありましたが、ここからが本題です。
以下は、【WordPress】YouTubeの背景動画が再生されない。 から色々引っ張ってきています。
header.php と footer.php は、先程の例を見本に用意しておいてください。その際、冒頭で指摘しています通り、script タグの記述は不要です。
functions.php
php
1 function teratail60093_scripts ( ) {
2 // Theme stylesheet.
3 wp_enqueue_style ( 'teratail60093-style' , get_stylesheet_uri ( ) ) ;
4 // Modal window.
5 wp_enqueue_script ( 'teratail60093-modalwindow' , get_stylesheet_directory_uri ( ) . '/js/modal.js' , array ( 'jquery' ) ) ;
6 }
7 add_action ( 'wp_enqueue_scripts' , 'teratail60093_scripts' ) ;
style.css
css
1 #modal-overlay {
2 z-index : 1 ;
3 display : none ;
4 position : fixed ;
5 top : 0 ;
6 left : 0 ;
7 width : 100 % ;
8 height : 100 % ;
9 background-color : rgba ( 255 , 255 , 255 , 0.75 ) ;
10 }
11
12 #modal-content {
13 width : 910 px ;
14 height : 360 px ;
15 margin : 0 ;
16 padding : 20 px 20 px ;
17 border : 0 px solid #aaa ;
18 background : #fff ;
19 position : fixed ;
20 /*display: none;*/
21 }
22
23 .button-link {
24 color : #00f ;
25 text-decoration : underline ;
26 }
27
28 .button-link :hover {
29 cursor : pointer ;
30 color : #f00 ;
31 }
32
33 .css_btn_class {
34 font-size : 16 px ;
35 font-family : Arial ;
36 font-weight : normal ;
37 -moz-border-radius : 8 px ;
38 -webkit-border-radius : 8 px ;
39 border-radius : 8 px ;
40 border : 0 px solid #d83526 ;
41 padding : 9 px 18 px ;
42 text-decoration : none ;
43 background : -moz-linear-gradient ( center top , #fe1a00 5 % , #ce0100 100 % ) ;
44 background : -ms-linear-gradient ( top , #fe1a00 5 % , #ce0100 100 % ) ;
45 filter : progid : DXImageTransform.Microsoft. gradient ( startColorstr= '#fe1a00' , endColorstr= '#ce0100' ) ;
46 background : -webkit-gradient ( linear , left top , left bottom , color-stop ( 5 % , #fe1a00 ) , color-stop ( 100 % , #ce0100 ) ) ;
47 background-color : #fe1a00 ;
48 color : #ffffff ;
49 display : inline-block ;
50 }
js/modal.js
javascript
1 ( function ( $ , window , document ) {
2 $ . fn . teratail60093ModalWindow = function ( ) {
3 // modal-overlay内にmodal-contentを配置しているためイベントバブルにより、
4 // modal-overlayのクリック(タッチ)イベントが発生してしまうためバブリングを停止させる
5 $ ( "#modal-content" ) . click ( function ( evt ) {
6 evt . stopPropagation ( ) ;
7 } ) ;
8
9 //リサイズされたら、センタリングをする
10 $ ( window ) . resize ( centeringModalSyncer ) ;
11
12 //ボタンのクリック(タッチ)イベント
13 $ ( ".modal-syncer" ) . click ( function ( ) {
14 //ボタンからフォーカスを外す
15 this . blur ( ) ;
16 //プレイヤー(iframe)のsrcにYouTube動画のURLを設定
17 $ ( "#vimeoPlayer" ) . attr ( "src" , "https://www.youtube.com/embed/" + $ ( this ) . data ( "videoId" ) + "?autoplay=0" ) ;
18 $ ( "#modal-overlay" ) . fadeIn ( "fast" ) ;
19 $ ( "#modal-content img" ) . attr ( "src" , $ ( this ) . data ( "description" ) ) ;
20 // センタリング
21 centeringModalSyncer ( ) ;
22 } ) ;
23
24 //[#modal-overlay]、または[#modal-close]をクリックしたら…
25 $ ( "#modal-overlay, #modal-close" ) . click ( function ( ) {
26 //[#modal-content]と[#modal-overlay]をフェードアウトした後に…
27 $ ( "#modal-overlay" ) . fadeOut ( "fast" , function ( ) {
28 $ ( "#vimeoPlayer" ) . attr ( "src" , "" ) ; // プレイヤーのsrcをクリア
29 } ) ;
30 } ) ;
31 } ;
32
33 //センタリングを実行する関数
34 function centeringModalSyncer ( ) {
35 //画面(ウィンドウ)の幅、高さを取得
36 var w = $ ( window ) . width ( ) ;
37 var h = $ ( window ) . height ( ) ;
38 //コンテンツ(#modal-content)の幅、高さを取得
39 var cw = $ ( "#modal-content" ) . outerWidth ( ) ;
40 var ch = $ ( "#modal-content" ) . outerHeight ( ) ;
41 //センタリングを実行する
42 $ ( "#modal-content" ) . css ( { left : ( ( w - cw ) / 2 ) , top : ( ( h - ch ) / 2 ) } ) ;
43 }
44
45 $ ( document ) . ready ( function ( ) {
46 $ ( document ) . teratail60093ModalWindow ( ) ;
47 } ) ;
48 } ) ( window . jQuery , window , document ) ;
こんな感じで動くと思います(動作確認済)。