简体   繁体   English

如何将codpen JS和AJax添加到我的一个WordPress页面中

[英]How can I add codpen JS and AJax to my one wordpress page

I just found a nice slider on codepen and want to integrate into my wp homepage (single page). 我刚刚在Codepen上找到了一个不错的滑块,并希望集成到我的wp主页(单页)中。

I have tried to export the Codepen slider and inject the inline JS and CSS. 我试图导出Codepen滑块并注入内联JS和CSS。 to the header using custom header code plugin and HTML code in the body.but got a conflict with other codes I already have on the website. 使用自定义标题代码插件和正文中的HTML代码将标题添加到标题。但是与我网站上已有的其他代码发生冲突。

My question is How Can I make that JS inline code affect only one DIV/ ID. 我的问题是如何使JS内联代码仅影响一个DIV / ID。

Thank you 谢谢

Don't rely on inline scripts. 不要依赖内联脚本。 Save them as separate files (hicham-slider.css/js) and store them somewhere on your site (probably /wp-content/themes/ACTIVETHEME/css/ and /wp-content/themes/ACTIVETHEME/js/ , create those directories if they don't exist. 将它们另存为单独的文件(hicham-slider.css / js)并将它们存储在您网站上的某个位置(可能是/wp-content/themes/ACTIVETHEME/css//wp-content/themes/ACTIVETHEME/js/ ,创建这些目录如果它们不存在。

Then you'll need to enqueue them with your functions.php file: 然后,您需要使用functions.php文件将它们加入队列

add_action( 'wp_enqueue_scripts', 'hicham_scripts' );
function hicham_scripts(){
    wp_enqueue_style( 'hicham-slider', get_stylesheet_directory_uri() .'/css/hicham-slider.css', [], '1.0' );
    wp_enqueue_script( 'hicham-slider', get_stylesheet_directory_uri() .'/js/hicham-slider.js', ['jquery'], '1.0', true );
}

Then put the HTML in a Custom HTML widget or in the html/text editor of a page or post. 然后将HTML放在“自定义HTML”小部件中或页面或帖子的html /文本编辑器中。

If the slider doesn't rely on jQuery, turn the ['jquery'] into [] instead. 如果滑块不依赖jQuery,则将['jquery']变为[] Read more about properly enqueueing with wp_enqueue_style() and wp_enqueue_script() 阅读有关使用wp_enqueue_style()wp_enqueue_script()正确入队的更多信息。

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

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