简体   繁体   English

在wordpress自定义主题中集成javascript文件

[英]Integrating javascript files in wordpress custom theme

I know that there already are several topics with this matter but none of them seem to resolve my problem. 我知道这个问题已经有几个话题了,但是似乎没有一个可以解决我的问题。

I want to link in a custom WordPress theme that I am creating several JavaScript files. 我想链接一个正在创建多个JavaScript文件的自定义WordPress主题。 I've tried a lot of possible ways and none of them seem to work for me. 我尝试了很多可能的方法,但似乎没有一种对我有用。

To skip all the ways I've already tried to add the files please tell me for example how can I add the file "prettyPhoto.js" in order to work in my custom theme. 要跳过我已经尝试添加文件的所有方法,请举例说明我如何添加文件“ prettyPhoto.js”以便在自定义主题中工作。

Thank you, Andi 谢谢你安迪

The right way is to use wp_register_script and wp_enqueue_script . 正确的方法是使用wp_register_scriptwp_enqueue_script Using these will instruct WordPress to load your script after any dependent scripts. 使用这些将指示WordPress在任何相关脚本之后加载您的脚本。

So say your prettyPhoto.js file is in mytheme/js . 假设您的prettyPhoto.js文件位于mytheme/js In functions.php , add something like: functions.php ,添加类似以下内容:

<?php
add_action('wp_enqueue_scripts', 'mytheme_load_scripts');
function mytheme_load_scripts() {
  // this only registers the script with WordPress
  wp_register_script('prettyPhoto', get_stylesheet_directory_uri().'/js/prettyPhoto.js');
  // this actually loads the script in `wp_head`
  wp_enqueue_script('prettyPhoto');
}
?>

If prettyPhoto.js is dependent on jQuery or something, then you specify this in wp_register_script . 如果prettyPhoto.js依赖于jQuery之类的东西,那么您可以在wp_register_script指定它。 Doing the below will tell WordPress to ensure jQuery is loaded before your script is loaded. 执行以下操作将告诉WordPress确保在加载脚本之前已加载jQuery。

<?php
wp_register_script('prettyPhoto', get_stylesheet_directory_uri().'/js/prettyPhoto.js', array('jquery'));
?>

See wp_register_script and wp_enqueue_script in the codex for more. 有关更多信息,请参见法典中的wp_register_scriptwp_enqueue_script

This article will probably help, too. 本文可能也会有所帮助。

I guess it didn't work for You, because You probably forgot about placing wp_head() 我想这对您不起作用,因为您可能忘记了放置wp_head()

immediately before tag in a theme template (ex. header.php, index.php) 在主题模板中标记之前(例如header.php,index.php)

or about placing wp_footer() 或关于放置wp_footer()

immediately before tag in a theme template (ex. footer.php, index.php). 在主题模板中标记之前(例如footer.php,index.php)。

Take some time and make yourself familiar with the Theme Development Checklist . 花一些时间让自己熟悉主题开发清单

It's hard to guess without seeing the template and without knowing what You've tried already, but what You need is the wp_enqueue_script function (in functions.php ) together with wp_head() and|or wp_footer() (in the template). 很难看到模板而不知道您已经尝试过的内容,但是您需要的是wp_enqueue_script函数(在functions.php )以及wp_head()和|或wp_footer()(在模板中)。

  1. Make sure, there is wp_head() right before </head> in the template. 确保模板中</head>之前有wp_head()
  2. Make sure, there is wp_footer() right before </body> in the template. 确保模板中的</body>前面有wp_footer()
  3. Place the following code in functions.php : 将以下代码放在functions.php

     function my_theme_add_javascript() { wp_enqueue_script( 'pretty-photo', get_template_directory_uri() . '/js/prettyPhoto.js', array('jquery') ); } add_action('wp_enqueue_scripts', 'my_theme_add_javascript'); 

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

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