是否有一些适用于 javascript 文件的 WordPress 模板简码?
我知道我们可以使用 获取 php 文件的路径,但这对于使用
enqueue_script函数? WordPress 是否在这些文件中更改了一些短代码?
编辑:我认为我没有清楚地表达我的需求。所以我想知道wordpress是否有一些短代码,放入使用enqueue方法加载的js文件中,将被模板路径替换。通常,我需要从模板中进行一些 ajax 调用,形成 .php 文件,并希望避免硬链接文件
I know we can get some path with <?php bloginfo('something');?>
into php files, but is it some equivalent for javascript file loaded with the enqueue_script
function ?
Did wordpress change some shortcode into those files ?
EDIT : I think I did not clearly express my needs. So i want to know if wordpress had some shortcode who, placed into a js file who is loaded with the enqueue method, will be replaced by the template path. Typically i need to make some ajax call form a .php file from my template and want to avoid hard linking file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有 javascript 文件不会被解析为 php,因此不会处理任何短代码或 php。
为什么不让你的链接具有相对性呢?我经常发现对我的开发副本进行子域名处理可以消除实时移动网站和损坏的链接时出现的任何问题。
您可以作弊并链接到一个 php 文件,然后该文件将标头信息作为 Javascript 传递。虽然看起来不太优雅。请参阅此处。
或者,您可以在一些内联 Javascript 中声明该变量,然后在外部 JS 文件中选取它。
然后在 yourscript.js 中引用变量“siteURL”
No javascript files won't be parsed as php, and as such won't process any shortcodes or php.
Why not just make your links relative. Often I find subdomaining my dev copy, removes any problems when moving a site live and broken links.
You could cheat and link to a php file, which then passes header information as Javascript. Doesn't seem very elegant though. See here.
Or you could just declare the variable in a little bit of inline Javascript and pick it up in the external JS file.
Then in yourscript.js just reference the variable 'siteURL'
您必须使用 wp_register_script() 注册脚本。然后通过将 wp_enqueue_script 放在 wp_head() 之前,它将为您加载它们。使用wp_enqueue_script的想法是,不需要手动将它们全部输入,并且可以根据某个脚本是否已加载来加载其他脚本。
You have to register scripts using wp_register_script(). Then by placing wp_enqueue_script before wp_head() it will load them in for you. The idea of using wp_enqueue_script is that you don't need to enter them all in manually, and you can load other scripts depending on whether a certain script has been loaded.