天天看點

wordpress主題架構thematic引入JS的方法

最近手上做的項目比較複雜,有關thematic架構的筆記或許從現在開始發表的更為混亂,不過大家不要擔心,這混亂隻不過是發表順序的混亂,文章内容絕對值得一看。

今天說一下利用thematic架構進行child theme開發時引用自定義JS檔案的方法。

thematic架構自帶了jquery和一個jquery插件,不過這遠遠不能滿足我們在實際開發中的需要,在實際開發中我們需要引入其他JS檔案怎麼辦?

首先強調一點,千萬不要去嘗試修改thematic架構中的函數和内容,我們隻需要在child theme中利用函數對thematic的輸出進行控制就好了。

例如今天我們要在名為thematicsamplechildtheme的child theme中引入一個tab.js檔案。

進入到child theme所在目錄,我這裡的路徑為E:\www\test\wp-content\themes\thematicsamplechildtheme,然後建立一個functions.php的檔案,在這個檔案裡輸入:

  1. function my_scripts($scripts) {  
  2.     $child_theme_directory = get_bloginfo('stylesheet_directory');  
  3.     $scripts .= "\n" . "\t";  
  4.     $scripts .= '<script src="'.$child_theme_directory.'/script/tab.js" type="text/javascript"></script>'."\n";  
  5.     $scripts .= "\n";  
  6.     return $scripts;  
  7. }  
  8. add_filter('thematic_head_scripts', 'my_scripts'); 

在以上代碼中,我們利用get_bloginfo('stylesheet_directory')來擷取這個child theme的路徑,因為是子主題,如果我們用get_bloginfo('template_directory')的話,那取得的将是themeatic的路徑了

然後我們将tab.js放入wp-content/themes/thematicsamplechildtheme/script/即可

回到wordpress前台重新整理代碼後,檢視一下網頁源檔案,是不是多了一行:

  1. <script src="http://127.0.0.1/test/wp-content/themes/thematicsamplechildtheme/script/tab.js" type="text/javascript"></script> 

這樣一來,我們在完全不修改thematic架構的情況下,開始了基于thematic的征程!

轉載于:https://blog.51cto.com/thematic/725061