Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

javascript - How to add custom css/js to just one page in WordPress?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...