javascript 如何将自定义 css/js 添加到 WordPress 中的一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17639723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add custom css/js to just one page in WordPress?
提问by Mehmed
I made a separate html file with custom css and js files. I want to integrate it into a wordpress site. I can copy and paste the body part of the html, however I don't know how to add css and js files properly. If I modify header.php, it will add these files to all pages and I don't want that. What is the solution?
我用自定义的 css 和 js 文件制作了一个单独的 html 文件。我想将它集成到一个 wordpress 站点中。我可以复制和粘贴 html 的正文部分,但是我不知道如何正确添加 css 和 js 文件。如果我修改 header.php,它会将这些文件添加到所有页面,而我不希望那样。解决办法是什么?
回答by The Alpha
When you will add the page from the WordPress
back end, using Pages->Add new
from the menu, you have to give a title and using that title (also possible using slug and id) you can check is_page('page title here')
and then can add JavaScript
and css
files, like
当您将从增加的内容WordPress
后端,使用Pages->Add new
从菜单中,你必须给一个标题和(使用蛞蝓和ID也可以),您可以检查使用的标题is_page('page title here')
,然后可以添加JavaScript
和css
文件,如
add_action( 'wp_enqueue_scripts', 'addcssAndScripts');
function addcssAndScripts()
{
if ( is_page('page-title') )
{
wp_enqueue_script( 'your_script' );
wp_enqueue_style( 'your-style' );
}
}
paste this code in your functions.php
and check wp_enqueue_styleand wp_enqueue_scriptfor better understanding of these functions and usage, also is_pageand wp_enqueue_scriptsif needed.
将此代码粘贴到您的functions.php
并检查wp_enqueue_style和wp_enqueue_script以更好地理解这些功能和用法,如果需要,还可以查看 is_page和wp_enqueue_scripts。