[英]Including typed.js in wordpress
I have been trying to add a typed.js plugin to one of the wordpress themes. 我一直在尝试将typed.js插件添加到wordpress主题之一。
I put the typed.js in the "js" folder in the theme folder. 我将typed.js放在主题文件夹中的“ js”文件夹中。 Then I edited the functions.php in the theme folder and added:
然后在主题文件夹中编辑functions.php并添加:
function typed_script_init() {
wp_enqueue_script( 'typedJS', get_template_directory_uri() . '/js/typed.js');
}
add_action('wp_enqueue_scripts', 'typed_script_init');
add_action('wp_enqueue_scripts', 'typed_init_in_footer');
function typed_init_in_footer() {
add_action('print_footer_scripts', 'typed_init');
}
function typed_init() { ?>
<script type="text/javascript">
$(function(){
$("#typed").typed({
strings: ["Are you Interested?"],
typeSpeed: 20
});
});
</script>
<?php }
I do have in one of my section in the theme: 我的主题中确实有一部分内容:
<h1>
<span id="typed"></span>
</h1>
And when I launch my wordpress page it does not work. 当我启动我的wordpress页面时,它不起作用。 I have been sitting on this for almost 3hours reading online tutorials about including javascript in wordpress and still nothing.
我已经坐了将近3个小时,阅读有关在WordPress中包含javascript的在线教程,但仍然一无所获。
Is there anybody who can help me? 有谁可以帮助我吗? Thanks guys!
多谢你们!
The proper way to do this would be to (1) include jQuery as a dependency (you haven't mentioned if you've included jQuery), (2) use noConflict wrappers : 做到这一点的正确方法是(1)将jQuery作为依赖项(如果您已经包含jQuery,则没有提到),(2)使用noConflict包装器 :
function typed_script_init() {
wp_enqueue_script( 'typedJS', get_template_directory_uri() . '/js/typed.js', array('jquery') );
}
add_action('wp_enqueue_scripts', 'typed_script_init');
function typed_init() {
echo '<script type="text/javascript">
(function($){
// The `$` jQuery shortcut is now available to you
$(function(){
$("#typed").typed({
strings: ["Are you Interested?"],
typeSpeed: 20
});
});
})(jQuery);
</script>';
}
add_action('wp_footer', 'typed_init');
I've added the scripts to the wp_footer
action hook...but it may be preferable to put those scripts in an external JS file, and also enqueue them in wp_enqueue_scripts
, with typedJS
as a dependency. 我已经将脚本添加到
wp_footer
动作钩子中了...但是最好将这些脚本放入外部JS文件中,并将它们放入wp_enqueue_scripts
,并将typedJS
作为依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.