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

时间:2021-12-21 22:55:29

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,它会将这些文件添加到所有页面,我不希望这样。解决办法是什么?

1 个解决方案

#1


10  

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 from菜单,您必须给出一个标题并使用该标题(也可以使用slug和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_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_style和wp_enqueue_script以更好地理解这些函数和用法,如果需要,还可以使用is_page和wp_enqueue_scripts。

#1


10  

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 from菜单,您必须给出一个标题并使用该标题(也可以使用slug和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_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_style和wp_enqueue_script以更好地理解这些函数和用法,如果需要,还可以使用is_page和wp_enqueue_scripts。