JS var 获取 WP 中的 PHP 值 [英] JS var to get PHP value in WP

查看:20
本文介绍了JS var 获取 WP 中的 PHP 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读所有类似的问题和答案,但似乎没有一个对我有用.

I was reading all similar questions and answers around, but none seem to work for me.

我有这个:

<script>
var MyCity  = "<?php echo 'Cityville';?>";
</script>

但这会输出整个 php.ini 文件.我也试过这个:

but this outputs the entire php. I also tried this:

<?php echo json_encode('City'); ?>

所有这些都在一个 WP 小部件中.

all these in a WP widget.

有什么想法吗?

谢谢!

推荐答案

问题是你的 echo 语句周围有引号,你需要在变量被输出到脚本中运行后,将它们放在你的变量周围,就像这样:

The issue is that you have quotes around your echo statement, you need to have them around your variable AFTER it has been output to function within the script, like so:

<script>
    var MyCity  = <?php echo '"Cityville"';?>;
</script>

这篇关于JS var 获取 WP 中的 PHP 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆