最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add custom cssjs to just one page in WordPress? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Jul 14, 2013 at 13:15 MehmedMehmed 3,0405 gold badges43 silver badges65 bronze badges 1
  • 2 something like this: wordpress.stackexchange./questions/89494/… – Scott Simpson Commented Jul 14, 2013 at 13:25
Add a ment  | 

1 Answer 1

Reset to default 13

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.

发布评论

评论列表(0)

  1. 暂无评论