【问题标题】:php constants in a javascript file | Add PHP Inside JSjavascript 文件中的 php 常量 |在 JS 中添加 PHP
【发布时间】:2021-02-28 16:33:24
【问题描述】:

我正在做一个测验项目。它需要修改 javascript 文件中的常量。

 const questions=[
   {
    q:'where is the capital of India',
    options:['New Delhi','Kolkatta','Varanashi','Agra'],
    answer:1
   },
   {
    q:'Who is the Prime Minister of India',
    options:['Amit Shah','Narendra Modi','Rahul Gandhi','None of the above'],
    answer:2
   },
   {
    q:'where is the capital of India',
    options:['New Delhi','Kolkatta','Varanashi','Agra'],
    answer:1
   }
]

但是我们需要在常量上添加 PHP。

试试这个,但是不行……

      const questions=[
  {
    q:'<?php echo the_field("q_1"); ?>',
    options:['(<?php echo the_field("option_1"); ?>)','(<?php echo the_field("option_2"); ?>)','(<?php echo the_field("option_3"); ?>)','(<?php echo the_field("option_4"); ?>)'],
    answer:'<?php echo the_field("answer_1"); ?>'
   },
   {
    q:'<?php echo the_field("q_2"); ?>',
    options:['<?php echo the_field("option_1_2"); ?>','<?php echo the_field("option_2_2"); ?>','<?php echo the_field("option_3_2"); ?>','<?php echo the_field("option_4_2"); ?>'],
    answer:'<?php echo the_field("answer_2"); ?>'
   },
   {
    q:'<?php echo the_field("q_3"); ?>',
    options:['<?php echo the_field("option_1_3"); ?>','<?php echo the_field("option_2_3"); ?>','<?php echo the_field("option_3_3"); ?>','<?php echo the_field("option_4_3"); ?>'],
    answer:'<?php echo the_field("answer_3"); ?>'
   }
]

Here is the full code

【问题讨论】:

  • 你能打印这个吗 let q1 = '';控制台.log(q1);上常数变量
  • 我能知道'the_field'变量来自哪里吗?
  • 如果您尝试在 JavaScript 文件中运行 php 代码..这是不可能的。 PHP 是一种服务器端语言……它需要预处理……但 JavaScript 在前端执行。
  • @DasunManathunga 使用 WordPress 插件 ACF。这不是一个大而复杂的代码。只需要用php替换文本即可。
  • 如果它是一个 javascript 文件,那么不,您不能在其中放入 PHP 变量,因为该文件不会通过 PHP 处理器。你能做的最好的是将它放在 php 文件的脚本部分中,以便 javascript 可以选择它

标签: javascript php wordpress constants advanced-custom-fields


【解决方案1】:

如果您使用 PHP 打印您的 JavaScript,您可以使用以下代码获得相同的结果:

<?php

$questions = [
    [
        q => the_field("q_1"),
        options => [
            the_field("option_1"), 
            the_field("option_2"), 
            the_field("option_3"), 
            the_field("option_4")
        ],
        answer => the_field("answer_1")
    ],
    [
        q => the_field("q_2"),
        options => [
            the_field("option_1_2"), 
            the_field("option_2_2"), 
            the_field("option_3_2"), 
            the_field("option_4_2")
        ],
        answer => the_field("answer_2")
    ],
    [
        q => the_field("q_3"),
        options => [
            the_field("option_1_3"), 
            the_field("option_2_3"), 
            the_field("option_3_3"), 
            the_field("option_4_3")
        ],
        answer => the_field("answer_3")
    ],
    // ... more options here
];
$output = [];

foreach($questions as $question) {
    $q = new stdClass();
    $q->q = $question['q'];
    $q->options = $question['options'];
    $q->answer = $question['answer'];
    array_push($output, $q);
}

然后,稍后在您的 HTML 中执行类似的操作:

<script type="text/javascript">
    const $questions = <?php echo json_encode($output); ?>;

    console.log($questions);
</script>

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2011-03-15
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多