在.js文件中,我需要获取wordpress主题的模板目录,即我需要获取<?PHP bloginfo('template_directory')的返回值;?>在js文件中.
这个想法是这样的:
@H_403_3@var blogTemplateDir = "<?PHP bloginfo('template_directory');?>";怎么能实现这一目标?这样做的标准(wordpress)方式是什么?
正如Erik早先发布的那样,JavaScript文件的文件扩展名没有意义,因为它最终归结为内容.在开发过程中遇到这种情况时,我发现我需要将以下内容添加到所谓的JavaScript文件的顶部:
@H_403_3@<?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).