简体   繁体   English

Wordpress-如何将主题目录放在js文件中?

[英]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.

相关问题 如何将jquery库和js文件加载到wordpress主题中? - How do I load jquery library and a js file into wordpress theme? 如果我不需要 SSR,如何编译 JS 文件以在 WordPress 主题中使用 React 组件? - How to compile the JS file to use a React component in a WordPress theme if I do not need SSR? 如何在我的 wordpress 主题 (Understrap) 中实现自定义 JS? - How do I implement custom JS into my wordpress theme (Understrap)? 问:如何在 wordpress 主题中包含 js 文件 - Q:How can I include the js file in wordpress theme 如何将jQuery代码放在WordPress主题的外部文件中? - How do I put jQuery code in an external file in WordPress theme? 我如何在Javascript文件中引用Wordpress根目录 - How do I refer to the root Wordpress directory INSIDE a Javascript file 如何在Wordpress中添加一个min JS文件,然后在我的主题上使用脚本标签调用? - How do you add a min JS file into Wordpress to then call on using script tags on my theme? js文件未包含在wordpress的主题中 - js file is not including in a theme in wordpress 如何在wordpress主题中的每个帖子上使用Vanilla JS逐步增加动画延迟 - How do I incrementally increase animation delay with vanilla JS for each post on in a wordpress theme 如何在Wordpress子主题functions.php中正确排队retina.js? - How do I enqueue retina.js in a Wordpress child theme functions.php properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM