本文介绍了php 在 js 文件 Wordpress 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .js 文件中,我需要获取 Wordpress 主题的模板目录,即我需要获取 <?php bloginfo('template_directory');?> 的返回值. 在 js 文件中.

In a .js-file, I need to get the template-directory of a Wordpress theme, i.e. I need to get the return-value of <?php bloginfo('template_directory');?> within the js-file.

这个想法是这样的:

var blogTemplateDir = "<?php bloginfo('template_directory');?>";

如何实现?这样做的标准 (Wordpress) 方式是什么?

How can this be achieved? What is the standard (Wordpress) way of doing this?

推荐答案

正如 Erik 之前发布的,JavaScript 文件的文件扩展名是没有意义的,因为它最终归结为内容.在开发过程中遇到这种情况,我发现我需要在所谓的 JavaScript 文件的顶部添加以下内容:

As Erik earlier posted, the file extension for the JavaScript-file is meaningless, as it in the end all comes down to the content. Upon encountering this situation during development, I found that I needed to add the following to the top of the so called JavaScript-file:

<?php

   //Let's set the header straight
   header('Content-type: text/javascript');

   //Get the WP-specifics, so that we can use constants and what not
   $home_dir = preg_replace('^wp-content/plugins/[a-z0-9\-/]+^', '', getcwd());
   include($home_dir . 'wp-load.php');
?>

这确保您将定义放入您的 JavaScript 文件中,并且您也可以将上面的示例与主题一起使用(只需确保将 /plugins 更改为 /themes代替).

This ensures that you get the definitions into your JavaScript-file, and you can use the example above with themes too (just make sure you change the /plugins into /themes instead).

这篇关于php 在 js 文件 Wordpress 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:33