[英]Wordpress - How do I take the theme directory in a js file?
I have an external script to integrate in a Wordpress theme. 我有一个集成在Wordpress主题中的外部脚本。 This script has different mixed resources in it like js, php files, etc. It's something like:
该脚本中包含不同的混合资源,例如js,php文件等。它类似于:
/mytheme/myscript/
...main.php
...folder/main.js
Now, main.js
post to main.php
and uses something like: 现在,
main.js
发布到main.php
并使用类似以下内容:
var comet = new vpb_start_comet('main.php');
comet.connect();
My problem's here. 我的问题在这里。
Main.php
is not found because I should change it with the relative wordpress path, being in a subfolder of my theme. Main.php
因为我应该使用相对的wordpress路径来更改它,该路径位于主题的子文件夹中。 So how could I get_bloginfo('template_url').'/myfolder/main.php
in my main.js
file? 那么如何在
main.js
文件中get_bloginfo('template_url').'/myfolder/main.php
?
If your folder/main.js
is being included via the wp_enqueue_script()
function ( which it probably should be, as this is the proper method with WP to load scripts ), then you can easily add the theme directory uri as a variable using the `localize_script()' function like so: 如果您的
folder/main.js
是通过wp_enqueue_script()
函数包含的wp_enqueue_script()
可能应该包含,因为这是使用WP加载脚本的正确方法),那么您可以轻松地使用主题目录uri作为变量添加`localize_script()'函数如下:
wp_localize_script( 'script_handle', 'themeDirURI', get_template_directory_uri() ) ;
You would put this code after the wp_enqueue for the main.js
script. 您可以将此代码放在
main.js
脚本的main.js
。 This will make the theme directory available as a javascript variable named themeDirURI
, and the value will be something like: ` http://www.example.com/wp-content/themes/theme-name/ ' 这将使主题目录可作为名为
themeDirURI
的JavaScript变量themeDirURI
,其值将类似于:` http : //www.example.com/wp-content/themes/theme-name/ '
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.