如何在WordPress的仅一页中添加自定义css/js? [英] How to add custom css/js to just one page in WordPress?

查看:320
本文介绍了如何在WordPress的仅一页中添加自定义css/js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用自定义的css和js文件制作了一个单独的html文件.我想将其集成到wordpress网站中.我可以复制并粘贴html的正文部分,但是我不知道如何正确添加css和js文件.如果我修改header.php,它将把这些文件添加到所有页面中,但我不希望这样.解决办法是什么?

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?

推荐答案

当您使用菜单中的Pages->Add newWordPress后端添加页面时,您必须提供标题并使用该标题(也可以使用slug和id)),您可以检查is_page('page title here'),然后可以添加JavaScriptcss文件,例如

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' );
    }
}

将此代码粘贴到functions.php中,然后检查 wp_enqueue_style wp_enqueue_scripts .

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.

这篇关于如何在WordPress的仅一页中添加自定义css/js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆