PHP 函数内的 PHP 和 HTML 代码 - Wordpress 2GIS Shortcode

我正在尝试将 2GIS 地图放入 wordpress 短代码中。我无法使用 HTML 小部件,因为我需要 PHP,我将使用 PHP 从 WordPress 帖子字段中提取纬度和经度。这将在一个帖子页面中。我回显 HTML 代码,因为它应该位于 WordPress 简码的函数内部。但后来我在第 55 行收到错误。

解析错误:语法错误,意外的“map”(T_STRING),期望“;” 或第 55 行的 ','

请帮助我理解这一点。

<?php

function test_func(){

  echo '

    <html>

      <head>

          <title>API карт 2ГИС</title>

          <script src="https://maps.api.2gis.ru/2.0/loader.js?pkg=full"></script>

          <script type="text/javascript">

              var map;


              DG.then(function () {

                  map = DG.map('map', {                // line 55

                      center: [54.98, 82.89],

                      zoom: 13

                  });


                  DG.marker([54.98, 82.89]).addTo(map);

              });

          </script>

      </head>

      <body>

          <div id="map" style="width:800px; height:400px"></div>

      </body>

  </html>

  ';

}


add_shortcode('test_shortcode', 'test_func');

?>


Smart猫小萌
浏览 93回答 2
2回答

开心每一天1111

您已将字符串封装在单引号中,并在字符串内使用单引号,这会使编译器感到困惑。所以你需要做的是转义内部单引号(我在这里只显示一小段代码),就像......&nbsp;map&nbsp;=&nbsp;DG.map('map',使用 \ 转义单引号&nbsp;map&nbsp;=&nbsp;DG.map(\'map\',

繁星淼淼

这也可能有效,而且更容易,特别是如果您有更多引号:<?phpfunction test_func(){?>&nbsp; <html>&nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>API карт 2ГИС</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script src="https://maps.api.2gis.ru/2.0/loader.js?pkg=full"></script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var map;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DG.then(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map = DG.map('map', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // line 55&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center: [54.98, 82.89],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom: 13&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DG.marker([54.98, 82.89]).addTo(map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; &nbsp; </head>&nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="map" style="width:800px; height:400px"></div>&nbsp; &nbsp; &nbsp; </body>&nbsp; </html><?php}add_shortcode('test_shortcode', 'test_func');?>
打开App,查看更多内容
随时随地看视频慕课网APP