2013-10-28 36 views
0

我有一個頁面,我想要爲worpress中的特定頁面加載一個sytlesheet和一個JS文件。頁面不在Wordpress中加載樣式表或JS文件

我在tabContent.css位於名爲tabcontent - >模板的目錄的head部分中添加了以下代碼,該模板與我的functions.php文件位於同一目錄中,並且類似於js文件。

<link href="<?php echo bloginfo('stylesheet_directory');?>/tab-content/template1/tabcontent.css" rel="stylesheet" type="text/css" />

<script src="<?php echo bloginfo('stylesheet_directory');?>/tab-content/tabcontent.js" type="text/javascript"></script>

我怎樣才能正確地加載這些文件。

感謝

+0

我有點困惑。因此,如果您的主題文件夾是xyz,則位於「xyz/tabcontent.css」,「xyz/tab-content/tabcontent.css」,「xyz/tab-content/template1/tabcontent.css」中的'tabcontent.css'或「xyz/tab-content/template/tabcontent.css」? –

+0

@TomaszNguyen抱歉應該更清楚。它是xyz/tab-content/template1/tabcontent.css其中xyz是我的主題文件夾 – user2798091

+0

看來您使用的代碼很好。你可以看看http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri。 –

回答

1

這不加載JS和CSS文件在WordPress特定頁面的正確方法。您需要使用is_page()條件標籤,並使用wp_enqueue_script()和wp_enqueue_style()加載資產。這裏有一個例子:

<?php 
    if(is_page(Page ID, Page Title or Page Slug)) { 
     wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); 
     wp_enqueue_style($handle, $src, $deps, $ver, $media); 
    } 
?> 

這段代碼應該放在主題的functions.php文件中。 請查看:

這是建立一個WordPress主題或插件時,遵循最佳實踐非常重要。

相關問題