简体   繁体   English

如何在WordPress中只将一个页面添加自定义css / js?

[英]How to add custom css/js to just one page in WordPress?

I made a separate html file with custom css and js files. 我用自定义的css和js文件制作了一个单独的html文件。 I want to integrate it into a wordpress site. 我想将它整合到wordpress网站。 I can copy and paste the body part of the html, however I don't know how to add css and js files properly. 我可以复制并粘贴html的正文部分,但是我不知道如何正确添加css和js文件。 If I modify header.php, it will add these files to all pages and I don't want that. 如果我修改header.php,它会将这些文件添加到所有页面,我不希望这样。 What is the solution? 解决办法是什么?

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 ,您必须提供标题并使用该标题(也可以使用slug和id),您可以检查is_page('page title here')然后可以添加JavaScriptcss文件,如

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_style and wp_enqueue_script for better understanding of these functions and usage, also is_page and wp_enqueue_scripts if needed. 将此代码粘贴到functions.php并检查wp_enqueue_stylewp_enqueue_script以更好地理解这些函数和用法,如果需要,还可以使用is_pagewp_enqueue_scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM